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

305
Vistas
Updating a map field in firestore

I'm newbie and just starting coding a couple of months ago. I'm creating an app with react native with expo and I'm having trouble updating a map field into the document. So far, when I needed to update a filed(for example a name) I was doing the following:

const updateData = async () => {
        await updateDoc(doc(db,"users",uid),{
            name:name, 
            age: age,
        })

The problem is that I now have a map field which stores data in the following way:

The way that I have the app set-up, is that I have an array for the food items:

enter image description here

const [item, setItem] = useState([
        {Food: 'tomato', Calories: 20, Quantity: 1,},
        {Food: 'lettuce', Calories: 10, Quantity: 1,},
    ]);

and I would like to store it into a map field in the firestore document.

I thought that I would be able to use a for loop, so that I can populate each field of an array element into the map, but it seems that I'm unable to use a loop inside the await.

Is there like a workaround that I would be able to use in this case?

The for loop that is being highlighted in red:

        await updateDoc(doc(db,"user",uid),{
        for (let i = 0; i < item.length; i++) {
            
        }

    },
    { merge: true })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To convert the array into a map, try using reduce() as shown below:

const updatedMap = item.reduce((a, b) => {
  a[b.Food] = b;
  return a; 
}, {})

await updateDoc(doc(db, "user", uid), { food: updatedMap })

This will use food name as key in food map and test of the object as value.

about 4 years ago · Juan Pablo Isaza 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