Use Promise function in Angular 6 Data Binding












1














I'm having an issue where I am using an async function in Angular 6 data binding:



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>


The getAssignedUser() is an async function which fetches a document from Firestore and I want to display the assigned user's firstname which I received from Firestore. The only problem here is I can't display the firstname value OnInit. If I insert a button and add a click event, it displays the name.



Component:



async getAssignedUser(id): Promise<string> {
if (id != null) {
return this._usersService
.getUserById(id)
.then(data => {
this.assignedUser = data.user_fname;
return this.assignedUser;
})
} else {
return null;
}
}


Service:



getUserById(id): any {
let user: any;
user = this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
return user;
}
else {
console.log('No such document');
}
}).catch(function (error) {
console.log('Error getting document: ', error);
})
return user;
}


Any help please?










share|improve this question




















  • 2




    Can you provide us with a stackblitz fro your example to be more clear ?
    – Shorbagy
    Nov 10 at 23:48










  • What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:49












  • @user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
    – sleepz
    Nov 10 at 23:53












  • You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:55






  • 1




    That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
    – user184994
    Nov 11 at 0:01
















1














I'm having an issue where I am using an async function in Angular 6 data binding:



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>


The getAssignedUser() is an async function which fetches a document from Firestore and I want to display the assigned user's firstname which I received from Firestore. The only problem here is I can't display the firstname value OnInit. If I insert a button and add a click event, it displays the name.



Component:



async getAssignedUser(id): Promise<string> {
if (id != null) {
return this._usersService
.getUserById(id)
.then(data => {
this.assignedUser = data.user_fname;
return this.assignedUser;
})
} else {
return null;
}
}


Service:



getUserById(id): any {
let user: any;
user = this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
return user;
}
else {
console.log('No such document');
}
}).catch(function (error) {
console.log('Error getting document: ', error);
})
return user;
}


Any help please?










share|improve this question




















  • 2




    Can you provide us with a stackblitz fro your example to be more clear ?
    – Shorbagy
    Nov 10 at 23:48










  • What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:49












  • @user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
    – sleepz
    Nov 10 at 23:53












  • You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:55






  • 1




    That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
    – user184994
    Nov 11 at 0:01














1












1








1







I'm having an issue where I am using an async function in Angular 6 data binding:



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>


The getAssignedUser() is an async function which fetches a document from Firestore and I want to display the assigned user's firstname which I received from Firestore. The only problem here is I can't display the firstname value OnInit. If I insert a button and add a click event, it displays the name.



Component:



async getAssignedUser(id): Promise<string> {
if (id != null) {
return this._usersService
.getUserById(id)
.then(data => {
this.assignedUser = data.user_fname;
return this.assignedUser;
})
} else {
return null;
}
}


Service:



getUserById(id): any {
let user: any;
user = this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
return user;
}
else {
console.log('No such document');
}
}).catch(function (error) {
console.log('Error getting document: ', error);
})
return user;
}


Any help please?










share|improve this question















I'm having an issue where I am using an async function in Angular 6 data binding:



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>


The getAssignedUser() is an async function which fetches a document from Firestore and I want to display the assigned user's firstname which I received from Firestore. The only problem here is I can't display the firstname value OnInit. If I insert a button and add a click event, it displays the name.



Component:



async getAssignedUser(id): Promise<string> {
if (id != null) {
return this._usersService
.getUserById(id)
.then(data => {
this.assignedUser = data.user_fname;
return this.assignedUser;
})
} else {
return null;
}
}


Service:



getUserById(id): any {
let user: any;
user = this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
return user;
}
else {
console.log('No such document');
}
}).catch(function (error) {
console.log('Error getting document: ', error);
})
return user;
}


Any help please?







javascript angular typescript google-cloud-firestore angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 0:41









HDJEMAI

4,205143956




4,205143956










asked Nov 10 at 23:34









sleepz

133




133








  • 2




    Can you provide us with a stackblitz fro your example to be more clear ?
    – Shorbagy
    Nov 10 at 23:48










  • What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:49












  • @user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
    – sleepz
    Nov 10 at 23:53












  • You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:55






  • 1




    That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
    – user184994
    Nov 11 at 0:01














  • 2




    Can you provide us with a stackblitz fro your example to be more clear ?
    – Shorbagy
    Nov 10 at 23:48










  • What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:49












  • @user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
    – sleepz
    Nov 10 at 23:53












  • You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
    – user184994
    Nov 10 at 23:55






  • 1




    That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
    – user184994
    Nov 11 at 0:01








2




2




Can you provide us with a stackblitz fro your example to be more clear ?
– Shorbagy
Nov 10 at 23:48




Can you provide us with a stackblitz fro your example to be more clear ?
– Shorbagy
Nov 10 at 23:48












What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
– user184994
Nov 10 at 23:49






What is it you're trying to display? You're doing {{getAssignedUser(application.user_id)}}, but getAssignedUser returns a Promise. You could use an async pipe, like {{getAssignedUser(application.user_id) | async }}
– user184994
Nov 10 at 23:49














@user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
– sleepz
Nov 10 at 23:53






@user184994 I'm trying to display a string actually. I don't know how to get the string value from the Promise. It's working with a click event. To be even more precise, I'm actually using an id to retrieve data from another Firestore document and want to access that document values.
– sleepz
Nov 10 at 23:53














You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
– user184994
Nov 10 at 23:55




You can try using the async pipe to get the value out, which would be {{getAssignedUser(application.user_id) | async }}
– user184994
Nov 10 at 23:55




1




1




That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
– user184994
Nov 11 at 0:01




That;s one of the downsides of calling functions from within the template: the function is called for every change detection cycle. Instead, why not called getUserById in ngOnInit, and just use {{assignedUser}} in your template?
– user184994
Nov 11 at 0:01












2 Answers
2






active

oldest

votes


















0















Issue




The issue with making the call getAssignedUser from the html and expecting to return the value. This will not guarantee because getAssignedUser performs async operation. You had mentioned the async operation on getAssignedUser however does not returns any Observable or Promise.




Solution




You need to change in both services and component. Function should return the Promise to handle this case.



Service:



  getUserById(id): any {

return new Promise((resolve, reject) => {

this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
resolve(user);
}
else {
resolve(null); //<-- you can reject if you want.
}
}
}
}


Component:



async getAssignedUser(id): Promise<string> {
return this._usersService
.getUserById(id);
}


html



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)} | async}
</span>


Important : You should not function in html, it may lead to multiple call and will impact on the performance. Better would be to use function instead.




Note : code is written directly to stackoverflow editor so there could be typo or syntactical error. So please correct yourself.







share|improve this answer





















  • async is required only to observable, not for promises.
    – Suresh Kumar Ariya
    Nov 11 at 6:30










  • @SureshKumarAriya I tried, and it's not working :/
    – sleepz
    Nov 11 at 8:17










  • @sleepz - please create stackblitz demo to get it fix quicker and easier way.
    – Sunil Singh
    Nov 11 at 8:31



















0














Agree with @Sunil Singh Above Answer, small correction in template file. async operator is mainly used for observable. You can call without that it should work.



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>





share|improve this answer





















  • Not true, the async pipe also works with promises. Please check the description in the docs
    – user184994
    Nov 11 at 7:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244472%2fuse-promise-function-in-angular-6-data-binding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Issue




The issue with making the call getAssignedUser from the html and expecting to return the value. This will not guarantee because getAssignedUser performs async operation. You had mentioned the async operation on getAssignedUser however does not returns any Observable or Promise.




Solution




You need to change in both services and component. Function should return the Promise to handle this case.



Service:



  getUserById(id): any {

return new Promise((resolve, reject) => {

this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
resolve(user);
}
else {
resolve(null); //<-- you can reject if you want.
}
}
}
}


Component:



async getAssignedUser(id): Promise<string> {
return this._usersService
.getUserById(id);
}


html



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)} | async}
</span>


Important : You should not function in html, it may lead to multiple call and will impact on the performance. Better would be to use function instead.




Note : code is written directly to stackoverflow editor so there could be typo or syntactical error. So please correct yourself.







share|improve this answer





















  • async is required only to observable, not for promises.
    – Suresh Kumar Ariya
    Nov 11 at 6:30










  • @SureshKumarAriya I tried, and it's not working :/
    – sleepz
    Nov 11 at 8:17










  • @sleepz - please create stackblitz demo to get it fix quicker and easier way.
    – Sunil Singh
    Nov 11 at 8:31
















0















Issue




The issue with making the call getAssignedUser from the html and expecting to return the value. This will not guarantee because getAssignedUser performs async operation. You had mentioned the async operation on getAssignedUser however does not returns any Observable or Promise.




Solution




You need to change in both services and component. Function should return the Promise to handle this case.



Service:



  getUserById(id): any {

return new Promise((resolve, reject) => {

this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
resolve(user);
}
else {
resolve(null); //<-- you can reject if you want.
}
}
}
}


Component:



async getAssignedUser(id): Promise<string> {
return this._usersService
.getUserById(id);
}


html



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)} | async}
</span>


Important : You should not function in html, it may lead to multiple call and will impact on the performance. Better would be to use function instead.




Note : code is written directly to stackoverflow editor so there could be typo or syntactical error. So please correct yourself.







share|improve this answer





















  • async is required only to observable, not for promises.
    – Suresh Kumar Ariya
    Nov 11 at 6:30










  • @SureshKumarAriya I tried, and it's not working :/
    – sleepz
    Nov 11 at 8:17










  • @sleepz - please create stackblitz demo to get it fix quicker and easier way.
    – Sunil Singh
    Nov 11 at 8:31














0












0








0







Issue




The issue with making the call getAssignedUser from the html and expecting to return the value. This will not guarantee because getAssignedUser performs async operation. You had mentioned the async operation on getAssignedUser however does not returns any Observable or Promise.




Solution




You need to change in both services and component. Function should return the Promise to handle this case.



Service:



  getUserById(id): any {

return new Promise((resolve, reject) => {

this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
resolve(user);
}
else {
resolve(null); //<-- you can reject if you want.
}
}
}
}


Component:



async getAssignedUser(id): Promise<string> {
return this._usersService
.getUserById(id);
}


html



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)} | async}
</span>


Important : You should not function in html, it may lead to multiple call and will impact on the performance. Better would be to use function instead.




Note : code is written directly to stackoverflow editor so there could be typo or syntactical error. So please correct yourself.







share|improve this answer













Issue




The issue with making the call getAssignedUser from the html and expecting to return the value. This will not guarantee because getAssignedUser performs async operation. You had mentioned the async operation on getAssignedUser however does not returns any Observable or Promise.




Solution




You need to change in both services and component. Function should return the Promise to handle this case.



Service:



  getUserById(id): any {

return new Promise((resolve, reject) => {

this.afs.collection('agents').doc(id).ref.get().then(function (doc) {
if (doc.exists) {
user = doc.data();
console.log(user);
resolve(user);
}
else {
resolve(null); //<-- you can reject if you want.
}
}
}
}


Component:



async getAssignedUser(id): Promise<string> {
return this._usersService
.getUserById(id);
}


html



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)} | async}
</span>


Important : You should not function in html, it may lead to multiple call and will impact on the performance. Better would be to use function instead.




Note : code is written directly to stackoverflow editor so there could be typo or syntactical error. So please correct yourself.








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 3:43









Sunil Singh

6,1372626




6,1372626












  • async is required only to observable, not for promises.
    – Suresh Kumar Ariya
    Nov 11 at 6:30










  • @SureshKumarAriya I tried, and it's not working :/
    – sleepz
    Nov 11 at 8:17










  • @sleepz - please create stackblitz demo to get it fix quicker and easier way.
    – Sunil Singh
    Nov 11 at 8:31


















  • async is required only to observable, not for promises.
    – Suresh Kumar Ariya
    Nov 11 at 6:30










  • @SureshKumarAriya I tried, and it's not working :/
    – sleepz
    Nov 11 at 8:17










  • @sleepz - please create stackblitz demo to get it fix quicker and easier way.
    – Sunil Singh
    Nov 11 at 8:31
















async is required only to observable, not for promises.
– Suresh Kumar Ariya
Nov 11 at 6:30




async is required only to observable, not for promises.
– Suresh Kumar Ariya
Nov 11 at 6:30












@SureshKumarAriya I tried, and it's not working :/
– sleepz
Nov 11 at 8:17




@SureshKumarAriya I tried, and it's not working :/
– sleepz
Nov 11 at 8:17












@sleepz - please create stackblitz demo to get it fix quicker and easier way.
– Sunil Singh
Nov 11 at 8:31




@sleepz - please create stackblitz demo to get it fix quicker and easier way.
– Sunil Singh
Nov 11 at 8:31













0














Agree with @Sunil Singh Above Answer, small correction in template file. async operator is mainly used for observable. You can call without that it should work.



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>





share|improve this answer





















  • Not true, the async pipe also works with promises. Please check the description in the docs
    – user184994
    Nov 11 at 7:34
















0














Agree with @Sunil Singh Above Answer, small correction in template file. async operator is mainly used for observable. You can call without that it should work.



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>





share|improve this answer





















  • Not true, the async pipe also works with promises. Please check the description in the docs
    – user184994
    Nov 11 at 7:34














0












0








0






Agree with @Sunil Singh Above Answer, small correction in template file. async operator is mainly used for observable. You can call without that it should work.



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>





share|improve this answer












Agree with @Sunil Singh Above Answer, small correction in template file. async operator is mainly used for observable. You can call without that it should work.



<span *ngIf="application.status == 'in-progress'; else new">
{{getAssignedUser(application.user_id)}}
</span>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 6:32









Suresh Kumar Ariya

4,3931215




4,3931215












  • Not true, the async pipe also works with promises. Please check the description in the docs
    – user184994
    Nov 11 at 7:34


















  • Not true, the async pipe also works with promises. Please check the description in the docs
    – user184994
    Nov 11 at 7:34
















Not true, the async pipe also works with promises. Please check the description in the docs
– user184994
Nov 11 at 7:34




Not true, the async pipe also works with promises. Please check the description in the docs
– user184994
Nov 11 at 7:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244472%2fuse-promise-function-in-angular-6-data-binding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud