Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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. 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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your alist will return a 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. 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:

  • Promise
  • Get data with Cloud Firestore
  • Update fields in nested objects
about 4 years ago · Juan Pablo Isaza Relatório

0

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)) // 👈
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda