gpt4 book ai didi

javascript - Firebase/nodeJS - 从多个 Firebase 数据引用中获取数据

转载 作者:行者123 更新时间:2023-12-03 03:20:33 26 4
gpt4 key购买 nike

我正在构建一个从引用获取数据的 json api,然后使用引用数据内的 ID,我需要从另一个引用获取其他数据并将它们全部放入对象中。

  1. first reference
  2. second reference

我想从第一个引用中获取数据,并使用 dUid 从第二个引用中获取电子邮件。问题是异步调用,我不能只从一个表获取数据并等待另一个表完成,而且我无法从第一个引用获取所有数据,然后从第二个引用获取所有数据,因为我以回调其中一个引用,如下所示:

app.get('/trip-statistics', (request, response) => {
tripRef.once('value').then(
snap => response.status(200).send(extractTripInfo(snap))
);
});

最佳答案

Node 8 支持 async/await 语法,这应该使这变得相当容易:

// Note the `async` keyword in front of the function
app.get('/trip-statistics', async (request, response) => {

// we'll wait for the first promise, its value will be put into the `tripValue` variable
const trip = await tripRef.once('value').then(snap => snap.val())

// whenever the trip is resolved, we will create a new promise, which depends on `trip.dUid`
const driver = await driverRef.child(trip.dUid).once('value').then(snap => snap.val())

// when both promises are fulfilled, we can tell the response to send both objects
response.status(200).send({ trip, driver })
})

不要忘记捕获错误,您可以在 this article 中找到一些示例.

关于javascript - Firebase/nodeJS - 从多个 Firebase 数据引用中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46593078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com