Listen only to additions to a cloud firestore collection?

后端 未结 4 1514
甜味超标
甜味超标 2021-01-04 00:44

I\'ve noticed, when I try to use a realtime listener on a Collection in firestore, each time a new Document is added to the collection, the logic will be rerun, and I will d

4条回答
  •  别那么骄傲
    2021-01-04 00:52

    It seems that the onSnapshotis only for listening an entire document,which means it would return all of the fields in a document that you're listening. So you are not able to only retrieve the changed field. But you can do different things according to different types of change:

    xxxx.onSnapshot(function(querySnapshot){
         querySnapshot.docChanges.forEach(function(change){
                        if(change.type=="added"){//first time it will be triggered
    
                        }else if(change.type == "modified"){//modified
    
                        }else if(change.type == "removed"){//removed
    
    
                        }
        })
    })
    

    Hope this helps

提交回复
热议问题