简体   繁体   English

Firebase 云功能不更新 Firestore 数据库中的数据

[英]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.

相关问题 更新 firebase Firestore 中的数据 - updating data in firebase Firestore 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore 从 Java 中的云函数访问 Google Firebase 数据库中的数据 - Access data in Google Firebase database from a cloud function in Java 如何使用 firebase function 从云 Firestore 读取数据并循环并将其推送到列表 - How to read data from cloud firestore with firebase function and loop and push it to list 使用 firebase 函数将数据添加到 firestore 数据库 - Adding data to firestore database with firebase functions Cloud Firestore 和 Firebase 实时数据库有什么区别? - What's the difference between Cloud Firestore and the Firebase Realtime Database? Firebase 云存储规则能否针对 Firestore 数据进行验证? - Can Firebase Cloud Storage rules validate against Firestore data? 如何使用 firebase 云函数对 Firestore 数据进行逻辑处理 - How to use firebase cloud functions for logic on firestore data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM