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

138
Visualizações
Adding firebase documents to a list

so at the moment I have a collection that looks like: https://i.stack.imgur.com/lxYoA.png . So in this collection it has a list which consists of nested lists. But I basically want to do a for loop that will add all of this to just one list, but what I'm doing below is just adding an empty list to firebase?

In firebase, I've added individual lists to another list as the documents consist of lists which I have done below and it creates the screenshot that I have above.

const snapshot = await fire.firestore()
        .collection("groupsCategory")
        .doc(groupID)
        .collection('events')
        .doc(eventID)
        .collection("memberPicks").get()
        
        const data = snapshot.docs.map(doc => doc.data())
        snapshot.docs.map(doc => doc.data())



        const db = fire.firestore();
     
        const likesList = [];

        snapshot.docs.forEach((doc) => {
            likesList.push(doc.data())

        })

As you can see here, I'm trying to get each item in the list and have it in one list and not a list of nested lists. Where am I going wrong?

  const arr1 = likesList.flat();

        const newArr = [];
        for (let i = 0; i < arr1; i++) {
            newArr.push(arr1[i])
          }

            db.collection("eventLikes")
            .doc(eventID)
            .set({
              ActivityLikes: newArr
            })
            
        }
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

As far as I understand your question, you want to set a document which doesn't have a nested list of userLikes. To be able to achieve that you should iterate the data in which you're fetching the objects of userLikes and pushing it into an array. See code below:

var userLikes = [];

const snapshot = await db
  .collection("groupsCategory")
  .doc(groupID)
  .collection('events')
  .doc(eventID)
  .collection("memberPicks").get()
  
snapshot.docs.forEach((doc) => {
  // Gets the List of `ActivityLikes`
  const activityLikes = doc.data().ActivityLikes;
  
  // Iterate the List of `ActivityLikes`
  activityLikes.forEach((activityLike) => {
    // Push the data object of `userLikes` to the initiated array: `userLikes`
    userLikes.push(activityLike.userLikes[0]);
  })
})

// This sets the data as follows: 
// Map (ActivityLikes) > Array (userLikes) > Map (userLikes Object)
db.collection("eventLikes")
  .doc('eventID')
  .set({
    ActivityLikes: {userLikes}
  })

Added some comments on the code above to better understand.

Here's the screenshot of the result: enter image description here


Take a look at these documentation to better understand working with objects and arrays:

  • Working with objects
  • Arrays
about 4 years ago · Santiago Gelvez 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