0

I do have not any familiarity with node/Js but I have 9 functions that I need to use in Firebase cloud functions, I made a scheduled function on a firebase in nodejs but it's not working as expected.

Just for clarification that the document that I want to update is in this hierarchy in firestore:

users (collection) -> abcUser (document) -> others (collection) -> limiter (document)

Below is one of the functions which is running succefuly on google cloud but the document is not getting update/change:

exports.resetLimiter = functions.pubsub
.schedule("*/5 * * * *")
.onRun( async (context) => {
  const snapshot = await db.collection("users").get();
  for (let i = 0; i < snapshot.docs.length; i++) {
    await snapshot[i].query.get.collection("others").doc("limiter")
        .update({
          weeklyFlow: 0.0,
          monthlyFlow: 0.0,
        });
  }
});

Peace!

1 Answer 1

1

update() returns a promise, but you're ignoring them. This causes the function to complete without waiting for the updates to complete. You need to await each one before returning.

Also your function seems to be incorrect in the way it uses the Firestore API. There is no property get on DocumentSnapshot objects. I'll try to rewrite it anyway with await.

      for (var i = 0; i < snapshot.docs.length; i++) {
        await db.collection("others").doc("limiter")
            .update({
              weeklyFlow: 0.0,
              monthlyFlow: 0.0,
            });
      });
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the prompt reply. The problem is that the "others" collections is inside each document that we get. The "others" collection is an inner collection inside each user document, don't know how to solve that. its still now updating any document.
Updated the code portion! please see on the question but its still not working. The hierarchy in firestore is like this: users (collection) -> abcUser (document) -> others (collection) -> limiter (document) which I need to update.
That still doesn't look like valid use of the Firestore API. There is no property query on a document snapshot. I would expect there to be an error message in the logs.
Then how to reference the inner collection of the document properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.