firestore 查询等待嵌套 foreach 循环

发布于 2025-01-10 01:14:06 字数 650 浏览 1 评论 0原文

我正在尝试使用集合中所有文档的名称填充一个数组,并使用这些父文档中的所有子集合文档填充另一个数组。

  let data = []
  let names = []
  
  const suppliers = await db.collection('suppliers').get()
  
  suppliers.forEach(async supplier => {

    names.push({name: supplier.data().name, id: supplier.id })

    const deliveries = await db.collection('suppliers').doc(supplier.id).collection('deliveries').get()
    
    deliveries.forEach(delivery => {
      
      data.push(delivery.data())
    })
  })

  console.log(names) // Populated
  console.log(data) // Empty

问题是它不等待内部循环完成就执行外部代码。 names 数组已填充,但 data 数组为空。如何在执行外部代码之前完成嵌套循环?

I'm trying to populate an array with names from all documents in a collection, and also populate another array with all the subcollection documents from those parent documents.

  let data = []
  let names = []
  
  const suppliers = await db.collection('suppliers').get()
  
  suppliers.forEach(async supplier => {

    names.push({name: supplier.data().name, id: supplier.id })

    const deliveries = await db.collection('suppliers').doc(supplier.id).collection('deliveries').get()
    
    deliveries.forEach(delivery => {
      
      data.push(delivery.data())
    })
  })

  console.log(names) // Populated
  console.log(data) // Empty

The problem is that it doesn't wait for the inner loop to finish before executing the code outside. The names array gets populated but the the data array is empty. How can i make the nested loop finish before executing the outside code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强辩 2025-01-17 01:14:06

加载供应商的交货是一个异步操作。由于您正在加载所有供应商的交货,并且只想在所有供应商加载完毕后进行记录,因此您需要等待多个异步操作,这需要使用 Promise.all

let data = []
let names = []

const suppliers = await db.collection('suppliers').get()

const promises = suppliers.docs.map(supplier => {

  names.push({name: supplier.data().name, id: supplier.id })

  return db.collection('suppliers').doc(supplier.id).collection('deliveries').get()    
})

const allDeliveries = await Promise.all(promises)
allDeliveries.forEach((deliveries) => {
  deliveries.forEach(delivery => {      
    data.push(delivery.data())
  })
})

console.log(names)
console.log(data)

Loading the deliveries for a supplier is an asynchronous operation. Since you're loading the deliveries for all suppliers and only want to log once all of them are loaded, you need to wait for multiple asynchronous operations, which requires the use of Promise.all:

let data = []
let names = []

const suppliers = await db.collection('suppliers').get()

const promises = suppliers.docs.map(supplier => {

  names.push({name: supplier.data().name, id: supplier.id })

  return db.collection('suppliers').doc(supplier.id).collection('deliveries').get()    
})

const allDeliveries = await Promise.all(promises)
allDeliveries.forEach((deliveries) => {
  deliveries.forEach(delivery => {      
    data.push(delivery.data())
  })
})

console.log(names)
console.log(data)
鼻尖触碰 2025-01-17 01:14:06

Foreach 不支持异步回调。为了摆脱这个,你可以使用。

let data = [];
let names = [];
suppliers.then(async(supplier) => {
    names.push({name: supplier.data().name, id: supplier.id })
    const deliveries = await db.collection('suppliers')                                            
                               .doc(supplier.id)
                               .collection('deliveries')
                               .get()
    
    deliveries.forEach(delivery => {
      data.push(delivery.data())
    })
  })

Foreach doesn't support asynchronous callback. In order to get rid of this you can use.

let data = [];
let names = [];
suppliers.then(async(supplier) => {
    names.push({name: supplier.data().name, id: supplier.id })
    const deliveries = await db.collection('suppliers')                                            
                               .doc(supplier.id)
                               .collection('deliveries')
                               .get()
    
    deliveries.forEach(delivery => {
      data.push(delivery.data())
    })
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文