0

I'm attempting to send a cloud message to users who are subscribed to a topic. The problem is that I'm now getting an infinite loop on the updateRecord. I'm trying to catch it inside here if(newData.count == prevData.count) return null; but that doesn't work. I think what's happening is that I'm updating the record inside the firestore document which is then calling the update function again causing the loop.

exports.updateRecord = functions.firestore.document("records/{recordsID}").onUpdate((change, context) => {

    const newData = change.after.data();
    const prevData = change.before.data();
    if(newData.count == prevData.count) return null;
    let newCount = prevData.count + 1;

    const doc = admin.firestore().doc(`/records/{recordsID}`);
    doc.set({ count: newCount })

    // TODO: -  Requires testing
    var topic = "global";

    const payload = {
        data: {
            count: `${newCount}`,
            firebaseId: `${newData.firebaseId}`,
            type: "updateRecord",
        }  
    };

    return admin.messaging().sendToTopic(topic, payload)
    .then((response) => {
        console.log('Message sent successfully');
    })
    .catch((error) => {
        console.log('Error sending message:', error);
    });

});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user7684436
  • 697
  • 14
  • 39

1 Answers1

1

The line if(newData.count == prevData.count) return null; doesn't work becuse your code is always updating the document with an incremented count every time the function is called.

It's not clear from your question what the count in this record is supposed to do, or why you even need to update the document at all. Your also ignoring the promise from the update, which is another problem. You might want to think through the problem you're trying to solve here and explain carefully why you're doing what you're doing in this code.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Shame that you can't just check the value of `context.auth`/`context.authType` like in an RTDB trigger. – samthecodingman Jul 04 '19 at 15:42
  • The count takes the previous data value and increment by a value of 1. What I'm trying to achieve is update the count value on the data, the reason this happens here is down to multiple users will be updating this value from various states in their app and I can't trust their count will be accurate. Once the count has been updated on the document the function should send a cloud message and then escape out. Hope that helps clarify it. – user7684436 Jul 04 '19 at 15:53
  • You will need a different condition to determine when the loop of function invocations should terminate. – Doug Stevenson Jul 04 '19 at 15:55
  • Would the following work in this context: `const newData = change.after.data().count; const prevData = change.before && change.before.data() && change.before.data().count ? change.before.data().count : 0; if (prevData !== newData) { // Notify users } else { return null; }` – user7684436 Jul 04 '19 at 16:32
  • @DougStevenson would the above work? (forgot to tag and having seen your YT vid) – user7684436 Jul 04 '19 at 16:44