[英]Schedule cloud function to update a list of maps
I'm trying to write a scheduled cloud function to reset the value of "status" every day at 12 am.我正在尝试编写一个预定的云 function 以每天凌晨 12 点重置“状态”的值。 Here's my firestore structure:
这是我的 Firestore 结构:
I haven't really tried coding in javascript before but here's what I managed with my little knowledge:我以前没有真正尝试过在 javascript 中编码,但这是我用我的一点知识管理的:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();
exports.Rst = functions.pubsub.schedule("0 0 * * *").onRun((context) => {
const alist =
database.collection("SA1XAoC2A7RYRBeAueuBL92TJEk1")
.doc("afternoon").get().then((snapshot)=>snapshot.data["list"]);
for (let i=0; i<alist.length; i++) {
alist[i]["status"]=0;
}
database.collection("SA1XAoC2A7RYRBeAueuBL92TJEk1")
.doc("afternoon").update({
"list": alist,
});
return null;
});
I get the following error when I deploy this function:部署此 function 时出现以下错误:
Expected Result: Set the values of all "status" fields to 0.预期结果:将所有“状态”字段的值设置为 0。
Your alist
will return a Promise { <pending> }
.您的列表将返回
alist
Promise { <pending> }
。 It needs to be fulfilled with a value or rejected with a reason (error).它需要用一个值来实现或用一个原因(错误)拒绝。 You should use the
.then
method to fulfill or use the .catch
method to get any errors of all the pending promises.您应该使用
.then
方法来实现或使用.catch
方法来获取所有未决承诺的任何错误。 See code below for reference:请参阅下面的代码以供参考:
const collectionName = "SA1XAoC2A7RYRBeAueuBL92TJEk1";
const documentName = "afternoon";
// created a reference to call between functions
const docRef = database.collection(collectionName).doc(documentName);
// Initialized a new array that will be filled later.
const tasks = [];
// Gets the data from the document reference
docRef.get()
// Fulfills the promise from the `.get` method
.then((doc) => {
// doc.data.list contains the array of your objects. Looping it to construct a `tasks` array.
doc.data().list.forEach((task) => {
// Setting the status to 0 for every object on your list
task.status = 0;
// Push it to the initialized array to use it on your update function.
tasks.push(task);
})
docRef.update({
// The `tasks` structure here must be the same as your Firestore to avoid overwritten contents. This should be done as you're updating a nested field.
list: tasks
}, { merge: true });
})
// Rejects the promise if it returns an error.
.catch((error) => {
console.log("Error getting document:", error);
});
I left some comments on the code for better understanding.为了更好地理解,我在代码上留下了一些评论。
You may also wanna check these documentations:您可能还想查看这些文档:
It seems that alist
is an object that Firestore can't handle.看来
alist
是Firestore无法处理的object。 To get rid of any of the parts that Firestore can't handle, you can do:要摆脱 Firestore 无法处理的任何部分,您可以执行以下操作:
database.collection("SA1XAoC2A7RYRBeAueuBL92TJEk1")
.doc("afternoon").update({
"list": JSON.parse(JSON.stringify(alist)) // 👈
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.