NextJS:等待来自 Google Firestore 的数据

NextJS: Waiting for the data from Google Firestore

所以,我正在尝试从 Google Firestore 获取数据,并且我正在使用 NextJS 框架。

Index.getInitialProps = async function () {
  const db = await loadDB()
  let data = []
  db.firestore().collection('data').get().then(querySnapshot => {
    querySnapshot.forEach(doc => {
      data.push(doc.data())
      console.log(data)
    })
  })

  return {
    data
  }
}

所以,在我的 "server" 上,我得到了数据,但在我的索引组件中,它仍然是一个空数组...有人知道我在这里缺少什么来将数据获取到组件吗?

我假设它在某处等待...

如果您想知道丢失的 await 应该在哪里,只有一个地方。它只适用于承诺,get() returns 承诺,因为对 Firestore 的查询不会立即完成。

async function () {
  const db = await loadDB()
  let data = []
  const querySnapshot = await db.firestore().collection('data').get()
  querySnapshot.forEach(doc => {
    data.push(doc.data())
    console.log(data)
  })

  return {
    data
  }
})