Firestore - Listen to specific field change?

前端 未结 4 1916
星月不相逢
星月不相逢 2020-11-29 09:43

How can I listen to a specific field change with firestore js sdk ?

In the documentation, they only seem to show how to listen for the whole document, if any of the

4条回答
  •  被撕碎了的回忆
    2020-11-29 10:28

    As Doug mentioned above, the entire document will be received in your function. However, I have created a filter function, which I named field, just to ignore document changes when those happened in fields that I am not interested in.

    You can copy and use the function field linked above in your code. Example:

    export const yourCloudFunction = functions.firestore
    .document('/your-path')
      .onUpdate(
        field('foo', 'REMOVED', (change, context) => {
    
          console.log('Will get here only if foo was removed');
    
        }),
      );
    

    Important: The field function is not avoiding your function to be executed if changes happened in other fields, it will just ignore when the change is not what you want. If your document is too big, you should probably consider Doug's suggestion.

提交回复
热议问题