Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Some fields are not saved into firestore using ionic

I'm facing an issue, when I try to save my data into firebase, some of the fields are not saved into it, whereas I have the right values in the console.

I'm trying to store a food object that has the current user id and name as attribut and the form values, for the form values it's ok but not for the current user data.

Here is my function: food.service.ts

private foodCollection: AngularFirestoreCollection<FoodView>;
...
addFood(food: FoodView) {
this.loadingService.present();
let foodToAdd: FoodView = food;
this.authService.getUserData().subscribe((u) => {
  foodToAdd.cookerName = u.displayName;
  foodToAdd.cookerId = u.uid;
});

console.log(foodToAdd);
return this.foodCollection.add(foodToAdd).then(() => this.loadingService.dismiss());}

The function where the query is: from the AngularFireStoreCollection

/**
 * Add data to a collection reference.
 *
 * Note: Data operation methods are done on the reference not the 
           query. This means
 * when you update data it is not updating data to the window of 
           your query unless
 * the data fits the criteria of the query.
 */
 add(data: T): Promise<DocumentReference<T>>;

here is where my function is used: add-food.page.ts

addFood() {
this.setFood();
this.foodService.addFood(this.food).then(() =>{
  this.presentAlert();
  this.tagList = [];
  this.ingredientList = [];
  this.addFoodForm.reset();
}
);  }

my auth service where I get user data:

  getUserData() {
return this.afs.doc<User>(`users/${this.getUserId()}`).valueChanges();  }

this is my output in console for my food object before I try to store him into firebase: Food object output before saving

And this is the same object in firebase but whithout the needed information: missing field

As you can see, the fields, cookerName and cookerId are not available.

I'm using Ionic 5 with Angular 11.

How can I fix it ?

Thank you for your help

Edit:

I've finally solve this, the problem was in my loading service that have async function (present and dismiss), by removing this.loadingService.present() and this.loadingService.dismiss() it works well

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Solution

To solve this problem, I've delete function the LoadingService class. As they were asynchronous the callback from Firebase with the right updated food value was done before the food value was update.

I've change this this:

addFood(food: FoodView) {
this.loadingService.present();
let foodToAdd: FoodView = food;
this.authService.getUserData().subscribe((u) => {
  foodToAdd.cookerName = u.displayName;
  foodToAdd.cookerId = u.uid;
});

console.log(foodToAdd);
return this.foodCollection.add(foodToAdd).then(() => this.loadingService.dismiss());}

for this:

  async addFood(food: FoodView) {
    this.authService.getUserData().subscribe((u) => {
      food.cookerName = u.displayName;
      food.cookerId = u.uid;
    });

    return this.foodCollection.add(food);
  }

and it works pretty well, I just have to change how my loading service works

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!