It appears that you are not returning the Promise from your function - there is no return statement in the getToken function at all, so the function itself just returns undefined. The internal return statement you have will resolve the promise, which is good, but you have to handle that resolution.
If you return the promise like so:
getToken(user_id) {
return this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
if (doc.exists) {
console.log(doc.data().user_token); //displays the correct user_token value
return doc.data().user_token;
}
});
}
You should be able to access the user_token asynchronously when the promise resolves by doing the following:
getToken(user_id).then(user_token => {
handle_user_token_here(user_token);
});
Note: The function as modified will return a promise. Therefore, you cannot simply the following:
let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
handle_user_token_here(user_token); // will not work.
You can do this though:
let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
user_token.then(user_token => handle_user_token_here(user_token)); // will work