【发布时间】:2018-04-23 05:26:03
【问题描述】:
我有一个 HTTP Cloud 函数,可以查询和更新 Firestore 中的文档。基本上,该函数的作用是从集合中选择所有车辆,计算不同时间范围(24 小时、12 小时和 6 小时)的每辆车的能源使用量,汇总结果并使用结果更新另一个集合。
这就是我的 Firestore 的结构。
vehicles (collection)
----vehicle (document)
-----telemetry (subcollection) this is where the energy values will come from
到目前为止,这是我的脚本,我使用 Promise.all 来组合不同时间范围的所有查询,计算每辆车的能源使用量并将其加到运行总数中,但这并不好用。
exports.getAllEnergyUsage = functions.https.onRequest((req, res) => {
const store = admin.firestore()
store.collection('vehicles').get().then(snapshot => {
let allYesterdayResult = 0;
let allTwelveHourResult = 0;
let allSixHourResult = 0;
snapshot.forEach(doc => {
const data_dict = doc.data();
let queryResults = [];
const vehicle = data_dict.vehicle_name;
const vehicleRef = store.collection('vehicles').doc(vehicle);
const today = new Date();
const yesterday = new Date(today.getTime() - (24*60*60*1000));
const twelveHours = new Date(today.getTime() - (12*60*60*1000));
const sixHours = new Date(today.getTime() - (6*60*60*1000));
// console.log(today, sixHours);
var queries = [
vehicleRef.collection('telemetry').where('time_stamp', '<', today).where('time_stamp', '>', yesterday).get(),
vehicleRef.collection('telemetry').where('time_stamp', '<', today).where('time_stamp', '>', twelveHours).get(),
vehicleRef.collection('telemetry').where('time_stamp', '<', today).where('time_stamp', '>', sixHours).get()
]
for (var i = 0; i < queries.length; i++) {
queryResults.push(
queries[i]
)
}
Promise.all(queryResults)
.then(snapShot=> {
const yesterdayResult = result => getEnergy(result);
const twelveHourResult = result => getEnergy(result);
const sixHourResult = result => getEnergy(result);
allYesterdayResult += yesterdayResult(snapShot[0])
allTwelveHourResult += twelveHourResult(snapShot[1])
allSixHourResult +=sixHourResult(snapShot[2])
console.log("Done updating vehicle ", vehicle)
// return res.send({"Result" : "Successful!"})
}).catch(reason => {
console.log(reason)
// return res.send({"Result" : "Error!"})
});
})
var vehicle_summary = {
yesterday : allYesterdayResult,
twelveHourResult : allTwelveHourResult,
sixHourResult : allSixHourResult,
timestamp : FieldValue.serverTimestamp()
}
console.log(vehicle_summary);
store.collection('vehicles_summary').doc('energy').set(vehicle_summary);
})
return res.send({"Result" : "Successful!"})
});
这是结果,这不会给我我想要的能源使用摘要,因为据我了解,承诺仍在运行,但函数已经返回(如果我错了,请纠正我)。
RESPONSE RECEIVED FROM FUNCTION: 200, {"Result":"Successful!"}
info: { yesterday: 0,
twelveHourResult: 0,
sixHourResult: 0,
timestamp: FieldValue {} }
info: Running loop on each SnapShot=======
info: Running loop on each SnapShot=======
Running loop on each SnapShot=======
Done updating vehicle Vehicle2
Running loop on each SnapShot=======
Running loop on each SnapShot=======
info: Running loop on each SnapShot=======
Done updating vehicle Vehicle1
info: Running loop on each SnapShot=======
info: Running loop on each SnapShot=======
Running loop on each SnapShot=======
info: Done updating vehicle Vehicle3
知道如何实现这一点吗?我来自 Python,刚刚学习 node.js,我仍然非常同步地思考。
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions es6-promise