Can Cloud Firestore onSnapshot() only trigger on changes, and not get the initial state?

后端 未结 4 1604
故里飘歌
故里飘歌 2020-12-17 20:35

The documentation says:

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document s

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 21:14

    What also worked for me was to check for the document.readystate and then choose to act on the snapshot changes. Like this -

    db.collection("collectionName").onSnapshot( (snapshot) => {
        console.log(snapshot.docChanges())
        if( ["loaded","interactive", "complete"].indexOf(document.readyState) >=0 ){
            <-----Your code to act on the snapshot changes---->
    
    }
    
    

    Based on the readystate property documentation here - https://www.w3schools.com/jsref/prop_doc_readystate.asp

提交回复
热议问题