我正在尝试使用cron触发我的云功能,以便每隔几个小时查看一次我的数据库。我可以自动触发该函数,但是输出不是我期望的。我只是有点困惑为什么我无法从数据库中检索到任何内容,这意味着我无法注销console.log(“ refund”)。我目前在请求集合中有1个文档,并且有1个文件具有满足已回复== false的已回复字段。因此,我只是对如何正确执行此操作感到困惑,因为我认为它应该记录一次该记录?
exports.daily_job = functions.https.onRequest((req, res) => {
const key = req.query.key;
// Exit if the keys don't match.
if (!secureCompare(key, functions.config().cron.key)) {
console.log('The key provided in the request does not match the key set in the environment. Check that', key,
'matches the cron.key attribute in `firebase env:get`');
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
'cron.key environment variable.');
return null;
}
let db = admin.firestore()
let request = db.collection('request')
.where('replied', '==', false)
.get().then(function(querySnapshot){
querySnapshot.forEach(function(doc) {
console.log("refunded")
})
})
.catch(function (error) {
console.log('Error getting documents: ', error)
res.send('error');
})
res.send('finished refund');
return null;
});
答案 0 :(得分:0)
您不必等待get()
返回的诺言,这是异步的。就像您的代码一样,在查询完成后,整个函数会在有时间完成之前立即发送响应“完成退款”。发送响应后,该功能将终止。
仅在函数中所有异步工作完成后才需要发送客户端响应,这将在您的then()
和catch()
回调中。 >