Reputation: 457
I have the following code that log to console all my document data. The problem is that I only receive updates as the function runs. I like to know how to setup a listener that log to console when data changes
const db = firebase.firestore();
const todoRef = db.collection("todo");
const list = (date) => {
console.log(date);
return todoRef
.where("owner", "==", firebase.auth().currentUser.uid)
.where("date", "==", date)
.get()
.then((querySnapshot) => {
const data = [];
querySnapshot.forEach((doc) => {
const d = {
id: doc.id,
...doc.data(),
};
data.push(d);
});
return data;
});
};
console.log("database stuff", list("2021 - 07 - 12"));
Upvotes: 1
Views: 166
Reputation: 50930
There's a listen to multiple documents section in the documentation.
db.collection("cities").where("state", "==", "CA")
.onSnapshot((querySnapshot) => {
var cities = [];
querySnapshot.forEach((doc) => {
cities.push(doc.data().name);
});
console.log("Current cities in CA: ", cities.join(", "));
});
You just need to use onSnapshot
instead of get
.
Upvotes: 1