1. Are you ready for the Galaxy S20? Here is everything we know so far!

Firebase Cloud Functions - Detect Changes to List inside Document

Discussion in 'Android Development' started by TsubasaChronix, Jul 1, 2020.

  1. TsubasaChronix

    Thread Starter

    I have tried searching around to find how to best do this in Firestore (I am new to nosql/non relational databases). I recently discovered Cloud Functions as a way to send notifications to users of my app when changes have been made to a document in the database. That part is very clear, however I am not able to see the best way to do this for lists that are stored inside that document. Currently my structure has an Image document that was added to the Uploads collection. Each Image has it's own document ID of course. I am trying to send a notification when a new like has been added. My Cloud Function below is attempting to grab the old like list and compare it to the new like list to determine the new value (Or if any changes made at all).

    Code (Javascript):
    1. exports.sendNewLikeNotification = functions.firestore
    2.     .document('Uploads/{image_id}')
    3.     .onUpdate(event => {
    4.         const oldValue = event.before.data();
    5.         const newValue = event.after.data();
    7.         const oldLikes = oldValue.likes
    8.         const newLikes = newValue.likes
    10. /* Code that will comp[are like values */
    11.     });
    Am I headed in the right direction with this, or is there a better way of doing this? Will this path even work?


Share This Page