在 Firebase 中获取 API 调用的空对象列表
Getting list of empty objects for API calls in Firebase
我正在构建一个 firebase 应用程序,用于计算存储在我的实时数据库中的 "from" 和 "to" 地址之间的通勤时间。我想调用一个 API 来计算每次通勤,并且 return 这些通勤时间一旦计算完毕。我第一次使用 Promise.all
来描述这种行为。但是,最后,我的调用结果只得到空对象。有什么建议吗?
exports.updateCommutes = functions.https.onRequest((request, response) => {
const ref = admin.database().ref('/users/test_user/commutes');
return ref.once('value').then((snapshot)=>{
var urls = [];
const testURl = 'https://maps.googleapis.com/maps/api/distancematrix/json?key=XXXX&origins=Boston,MA&destinations=Cambridge,MA'; // I'm using a test API call for now
return Promise.all([fetch(testURl),fetch(testURl)])
.then((responses)=>{
return responses.map((res)=>res.json())
})
.then((data)=>{
console.log(data);// this prints out: [ Promise { <pending> }, Promise { <pending> } ]
return response.send(data);
})
})
.catch((error)=>console.log("error$"+error))
});
我的数据库是这样的:
icommute-firebase
- users
- - test_user
- - - commutes
- - - - M26JimVJ3P_nEvm1L6n
- - - - - from: Boston,MA
- - - - - to: Cambridge,MA
- - - - M26Joyu-rLGomY_oElm
- - - - M26JtYyvGK_fCx0FE-Q
- - - - M26Qhuc-gebRppy320c
Postman 向 http://localhost:5001/icommute-firebase/us-central1/updateCommutes 发出 GET 请求,结果:
[
{},
{}
]
预产期:
[
{
"destination_addresses": [
"Cambridge, MA, USA"
],
"origin_addresses": [
"Boston, MA, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "4.9 km",
"value": 4925
},
"duration": {
"text": "16 mins",
"value": 978
},
"status": "OK"
}
]
}
],
"status": "OK"
},
{
"destination_addresses": [
"Cambridge, MA, USA"
],
"origin_addresses": [
"Boston, MA, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "4.9 km",
"value": 4925
},
"duration": {
"text": "16 mins",
"value": 978
},
"status": "OK"
}
]
}
],
"status": "OK"
}
]
res.json()
也是 returns 一个承诺。所以你也需要处理它。请参阅以下示例:
const fetch = require('node-fetch');
const testURl = 'https://jsonplaceholder.typicode.com/todos/1'
return Promise.all([fetch(testURl),fetch(testURl)])
.then((responses)=>{
// res.json() returns a Promise.
// We need to wrap them again in Promise.all()
return Promise.all(responses.map((res)=>res.json()));
})
.then((data)=>{
console.log(data);
});
这记录了预期的输出:
[ { userId: 1,
id: 1,
title: 'delectus aut autem',
completed: false },
{ userId: 1,
id: 1,
title: 'delectus aut autem',
completed: false } ]
我正在构建一个 firebase 应用程序,用于计算存储在我的实时数据库中的 "from" 和 "to" 地址之间的通勤时间。我想调用一个 API 来计算每次通勤,并且 return 这些通勤时间一旦计算完毕。我第一次使用 Promise.all
来描述这种行为。但是,最后,我的调用结果只得到空对象。有什么建议吗?
exports.updateCommutes = functions.https.onRequest((request, response) => {
const ref = admin.database().ref('/users/test_user/commutes');
return ref.once('value').then((snapshot)=>{
var urls = [];
const testURl = 'https://maps.googleapis.com/maps/api/distancematrix/json?key=XXXX&origins=Boston,MA&destinations=Cambridge,MA'; // I'm using a test API call for now
return Promise.all([fetch(testURl),fetch(testURl)])
.then((responses)=>{
return responses.map((res)=>res.json())
})
.then((data)=>{
console.log(data);// this prints out: [ Promise { <pending> }, Promise { <pending> } ]
return response.send(data);
})
})
.catch((error)=>console.log("error$"+error))
});
我的数据库是这样的:
icommute-firebase
- users
- - test_user
- - - commutes
- - - - M26JimVJ3P_nEvm1L6n
- - - - - from: Boston,MA
- - - - - to: Cambridge,MA
- - - - M26Joyu-rLGomY_oElm
- - - - M26JtYyvGK_fCx0FE-Q
- - - - M26Qhuc-gebRppy320c
Postman 向 http://localhost:5001/icommute-firebase/us-central1/updateCommutes 发出 GET 请求,结果:
[
{},
{}
]
预产期:
[
{
"destination_addresses": [
"Cambridge, MA, USA"
],
"origin_addresses": [
"Boston, MA, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "4.9 km",
"value": 4925
},
"duration": {
"text": "16 mins",
"value": 978
},
"status": "OK"
}
]
}
],
"status": "OK"
},
{
"destination_addresses": [
"Cambridge, MA, USA"
],
"origin_addresses": [
"Boston, MA, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "4.9 km",
"value": 4925
},
"duration": {
"text": "16 mins",
"value": 978
},
"status": "OK"
}
]
}
],
"status": "OK"
}
]
res.json()
也是 returns 一个承诺。所以你也需要处理它。请参阅以下示例:
const fetch = require('node-fetch');
const testURl = 'https://jsonplaceholder.typicode.com/todos/1'
return Promise.all([fetch(testURl),fetch(testURl)])
.then((responses)=>{
// res.json() returns a Promise.
// We need to wrap them again in Promise.all()
return Promise.all(responses.map((res)=>res.json()));
})
.then((data)=>{
console.log(data);
});
这记录了预期的输出:
[ { userId: 1,
id: 1,
title: 'delectus aut autem',
completed: false },
{ userId: 1,
id: 1,
title: 'delectus aut autem',
completed: false } ]