Listen to meta data firestore

孤者浪人 提交于 2021-01-29 13:01:22

问题


I am new to Firestore and learning things out. On my learning path, I have reached the section on Events for meta data changes in the Firebase documentation.

This is looking very useful but I am unable to understand how to test it. This is the code in documentation

db.collection("cities").doc("SF")
    .onSnapshot({
        // Listen for document metadata changes
        includeMetadataChanges: true
    }, function(doc) {
        // ...
    });

I added my simple update command to see what happens, it is updating every second. I want to understand what it is trying to give me back ? and in what case I can use it ? Why is it updating every second ?

 firebase.firestore().collection("cities").doc("DC")
    .onSnapshot({
        // Listen for document metadata changes
        includeMetadataChanges: true
    }, function(doc) {
        // ...
    var docRef = firebase.firestore().collection('cities').doc('DC');
     var updateTimestamp = docRef.update({
     timestamp: firebase.firestore.FieldValue.serverTimestamp()
 });
    });

回答1:


So actually you created modern infinite loop.

You subscribed to snapshotChanges of DC document (for data and metadata), so it means any change in the document you will receive it. And as soon as you run it for the first time, the current data comes as first time run subscription.

Then in subscribe, you update same document, it means again your subscription will be run, and again and again.

 firebase.firestore().collection("cities").doc("DC")
    .onSnapshot({includeMetadataChanges: true}, (docSnapshot) => {
        console.log(docSnapshot);
    });


来源:https://stackoverflow.com/questions/60830868/listen-to-meta-data-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!