Home > Mobile >  Schedule cloud function to update a list of maps
Schedule cloud function to update a list of maps

Time:03-22

I'm trying to write a scheduled cloud function to reset the value of "status" every day at 12 am. Here's my firestore structure: Firestore Data

I haven't really tried coding in javascript before but here's what I managed with my little knowledge:

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: Error

Expected Result: Set the values of all "status" fields to 0. Expected result

CodePudding user response:

It seems that alist is an object that Firestore can't handle. To get rid of any of the parts that Firestore can't handle, you can do:

database.collection("SA1XAoC2A7RYRBeAueuBL92TJEk1")
    .doc("afternoon").update({
      "list": JSON.parse(JSON.stringify(alist)) //            
  • Related