最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to iterate through a Firestore snapshot documents while awaiting - Stack Overflow

programmeradmin5浏览0评论

I have been trying to obtain a series of documents from firestore, reading them and acting accordingly depending on a series of fields. The key part is I want to wait for a certain process while working on each document. The official documentation presents this solution:

const docs = await firestore.collection(...).where(...).where(...).get()
    docs.forEach(await (doc) => {
      //something
    })

The problem with this solution is taht when you have a promise inside the forEach it won't await it before continuing, which I need it to. I have tried using a for loop:

const docs = await firestore.collection(...).where(...).where(...).get()
            for(var doc of docs.docs()) {
      //something
            }

When using this code Firebase alerts that 'docs.docs(...) is not a function or its return value is not iterable'. Any ideas on how to work around this?

I have been trying to obtain a series of documents from firestore, reading them and acting accordingly depending on a series of fields. The key part is I want to wait for a certain process while working on each document. The official documentation presents this solution:

const docs = await firestore.collection(...).where(...).where(...).get()
    docs.forEach(await (doc) => {
      //something
    })

The problem with this solution is taht when you have a promise inside the forEach it won't await it before continuing, which I need it to. I have tried using a for loop:

const docs = await firestore.collection(...).where(...).where(...).get()
            for(var doc of docs.docs()) {
      //something
            }

When using this code Firebase alerts that 'docs.docs(...) is not a function or its return value is not iterable'. Any ideas on how to work around this?

Share Improve this question edited Aug 9, 2020 at 17:23 NumberC 5967 silver badges17 bronze badges asked Aug 9, 2020 at 15:27 fds18fds18 1181 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 21

Note that your docs variable is a QuerySnapshot type object. It has an array property called docs that you can iterate like a normal array. It will be easier to understand if you rename the variable like this:

const querySnapshot = await firestore.collection(...).where(...).where(...).get()
for (const documentSnapshot of querySnapshot.docs) {
    const data = documentSnapshot.data()
    // ... work with fields of data here
    // also use await here since you are still in scope of an async function
}

I have found this solution.

const docs = [];

firestore.collection(...).where(...).get()
    .then((querySnapshot) => {
        querySnapshot.docs.forEach((doc) => docs.push(doc.data()))
    })
    .then(() => {
        docs.forEach((doc) => {
            // do something with the docs
        })
    })

As you can see this code stores the data in an extern array and only after this action it works with that data

I hope this helped you to solve the problem!

发布评论

评论列表(0)

  1. 暂无评论