Currently, I am working on an Angular app and facing a challenge. After receiving a user variable from an asynchronous call to Firestore Cloud, I noticed that the variable is successfully set (verified using console.log()). However, when I navigate between pages and attempt to load the recipes associated with that user on Firestore, it indicates that the user variable is undefined.
Is there a more effective approach to handle this situation?
Thank you
EDIT: Code
tryConnect(email: string, password: string) {
// u = array of users
this.usersWithId.subscribe(u => {
for (const a of u) {
console.log(a);
if (a.email === email && a.password === password) {
this.connectedUser = a;
console.log(this.connectedUser);
break;
}
}
});
}
addRecipe(recipe: Recipe) {
console.log(this.connectedUser);
this.db.collection('/recipes').add({
name: recipe.name,
description: recipe.description,
meal: recipe.meal,
steps: recipe.steps,
servings: recipe.servings,
cuisine: recipe.cuisine,
userID: this.connectedUser.userID,
ingredients: recipe.ingredients
});