Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda