0

I would like to know if it's possible to detect when a new fields is added on a document with cloud functions. I store all my friends in a single document with this structure :

- userId
    - friends_sub_collection
        - friendsListDocument
            friendId0 (HashMap<String, Object>)
               "status": "friend_request",
               "date" : 1583448203,
               ...
            friendId1
            friendId2
            ...

I know that i can get the changes in cloud functions with something like :

.onUpdate((change, context) => {

  const newValue = change.after.data();

  const previousValue = change.before.data();
}

But since my fields are documentId i can't check if they exists with something like newValue.friendsFieldId

My suggestion will be to store all ids in an array of the document allFriendsId, then check the difference between the old and new array and then get the right field.

Is there any better ways ?

Thanks for the suggestions and your time.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Tom
  • 133
  • 2
  • 11
  • This is a standard javascript problem related to comparing two JS objects. There's nothing special about cloud functions or firestore here. There are lots of things you can search for and implement. – Doug Stevenson Mar 05 '20 at 23:00
  • Yeah you are totally right sorry, thanks for the answer. – Tom Mar 05 '20 at 23:23

0 Answers0