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);
});
});