Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda