【发布时间】:2018-07-16 10:51:41
【问题描述】:
我正在尝试使用 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;
});
【问题讨论】:
-
我没有看到“需要退款”日志行。
-
抱歉刚刚改了 console.log("refunded")
-
所以奇怪的事情发生了,所以它退出了,函数执行花了 2916 毫秒,完成状态码:200,然后它退出“退款”
-
所以它是在执行之后来的?我的意思是我猜它是否是异步的,但我没有让它等待..
-
它可能根本就不会回调。发送响应后,Cloud Functions 将关闭一切。
标签: javascript firebase firebase-realtime-database google-cloud-functions