简体   繁体   English

将 firebase 个文档添加到列表中

[英]Adding firebase documents to a list

so at the moment I have a collection that looks like: https://i.stack.imgur.com/lxYoA.png .所以目前我有一个看起来像这样的集合: 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?但我基本上想做一个 for 循环,将所有这些添加到一个列表中,但我在下面做的只是将一个空列表添加到 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.在 firebase 中,我将单独的列表添加到另一个列表,因为文档由我在下面完成的列表组成,它创建了我上面的屏幕截图。

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
            })
            
        }

As far as I understand your question, you want to set a document which doesn't have a nested list of userLikes .据我了解你的问题,你想设置一个没有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.为了能够实现这一点,您应该迭代要在其中获取userLikes对象并将其推入数组的数据。 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:这是结果的屏幕截图: 在此处输入图像描述


Take a look at these documentation to better understand working with objects and arrays:查看这些文档以更好地理解使用对象和 arrays:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列出子集合中所有文档的名称 - Firebase Flutter - List names of all documents in subcollection - Firebase Flutter 如何检索 Firebase 中集合中的所有文档并添加到列表中? - How to retrieve all documents in a collection in Firebase and add to a list? Flutter/Firebase - 列表<dynamic>没有实例 getter 'documents'</dynamic> - Flutter/Firebase - List<dynamic> has no instance getter 'documents' Flutter Firebase:检索文档列表,仅限于数组中的 ID? - Flutter Firebase: Retrieve a list of documents, limited to IDs in an array? Flutter Firebase Cloud Firestore 通过 ID 获取文档列表 stream - Flutter Firebase Cloud Firestore get stream of list of documents by their ids 无法从 Firebase 子集合 Flutter 中获取文档列表 - Not able to get a list of documents from Firebase Subcollection Flutter firebase文件如何订购? - How to order the documents of firebase? Flutter Firebase 遍历匹配字段值的所有文档,并将每个文档的字段插入字符串列表 - Flutter Firebase Iterate through all documents that matches field value and inserting field from each one into a List of String 当文档包含 Firebase firestore 中的嵌套文档时获取文档名称列表 - Get documents names list when document include nested document in Firebase firestore Firebase函数中查询嵌套文档 - Query nested documents in Firebase functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM