[英]Firebase Cloud function not updating data in Firestore database
So I'm writing a scheduled cloud function that is supposed to run every minute and update certain values in my Firestore database.所以我正在编写一个预定的云函数,它应该每分钟运行一次并更新我的 Firestore 数据库中的某些值。 I'm getting a success on the function and I don't see any errors, however, the database isn't updating.我在该功能上取得了成功,并且没有看到任何错误,但是,数据库没有更新。 My project is made with flutter but I wrote the cloud function with node.js我的项目是用 flutter 制作的,但我用 node.js 编写了云函数
enter image description here在此处输入图像描述
This is my cloud function code.这是我的云功能代码。
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.myScheduledCloudFunction = functions.pubsub.schedule('* * * * *').timeZone('Asia/Kuala_Lumpur').onRun(async (context) => { admin.firestore().collection('users').get().then(function(querySnapshot) { querySnapshot.forEach(function(doc) { admin.firestore().collection('users').doc(doc.id).collection('habits').get().then(function(querySnapshot) { querySnapshot.forEach(function(habitDoc) { admin.firestore().collection('users').doc(doc.uid).collection('habits').doc(habitDoc.id).ref.update({ 'iscompleted': false, 'completedcount': 0 }) }) }) }) }) return null; });
I feel like it might be related to the rules but I'm not sure, for now my rules are default I haven't changed them.我觉得这可能与规则有关,但我不确定,因为现在我的规则是默认的我没有更改它们。 I hope someone can help我希望有人能帮帮忙
Thanks to @Doug Stevenson I've learned that I wasn't giving any time for the async functions to take its course.感谢@Doug Stevenson,我了解到我没有给异步函数任何时间来完成它的过程。 I have fixed my work and now it is working.我已经修复了我的工作,现在可以正常工作了。
here's the fixed code:这是固定代码:
exports.myScheduledCloudFunction = functions.pubsub.schedule('* * * * *').timeZone('Asia/Kuala_Lumpur').onRun(async (context) => { const querySnapshot = await admin.firestore().collection('users').get(); querySnapshot.forEach(async (doc) => { const habitQuerySnapshot = await admin.firestore().collection('users').doc(doc.id).collection('habits').get(); habitQuerySnapshot.forEach(async (habitDoc) => { if (doc && doc.ref) { await habitDoc.ref.update({ 'iscompleted': false, 'completedcount': 0 }); } }); }); return null; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.