问题
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