【发布时间】:2020-04-01 08:16:43
【问题描述】:
我有一个使用where 和get 从Firestore 获取数据的函数。问题是如果添加新记录,它不会实时推送到客户端。我知道我必须使用.valueChanges(),但我还需要使用.where() 条件获取数据,因为我无法获取整个数据
我只需要获取logged in user 的数据。这就是为什么需要where 条件的原因。
如何同时使用两者?
这是我当前的代码:
assign_by(){
this.goalList =[];
this.storage.get('loggedInUser').then((val) => {
console.log('Your user ID is', val);
this.loggedInusr = val;
firebase.firestore().collection(`todos`)
//.where("assignTo", "==", this.loggedInusr)
.where("assignBy", "==", this.loggedInusr)
//.where("followers", "array-contains", this.loggedInusr)
.get()
//.valueChanges()
.then(querySnapshot => {
querySnapshot.forEach(doc=> {
// doc.data() is never undefined for query doc snapshots
console.log("assignby");
console.log(doc.id, " ===> ", doc.data());
this.goalList.push(doc.data());
});
});
});
}
编辑 1
按照指导,我已将当前代码更改如下:
assign_to(){
this.goalList =[];
this.storage.get('loggedInUser').then((val) => {
console.log('Your user ID is', val);
this.loggedInusr = val;
firebase.firestore().collection(`todos`)
.where("assignTo", "==", this.loggedInusr)
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.data());
console.log("assignby");
console.log(doc.id, " ===> ", doc.data());
this.goalList.push(doc.data());
});
});
});
}
新记录由登录用户添加,因此新记录应显示在此屏幕上,显示该用户分配的所有记录。
我添加了这条记录:
但是这里没有更新:
编辑 2
如上图所示的assign_by()函数来自这个函数。请注意,如果我再次打开此页面,可以看到新记录。
assign_by(){
this.goalList =[];
this.storage.get('loggedInUser').then((val) => {
console.log('Your user ID is', val);
this.loggedInusr = val;
firebase.firestore().collection(`todos`)
//.where("assignTo", "==", this.loggedInusr)
.where("assignBy", "==", this.loggedInusr)
// .get()
// .then(querySnapshot => {
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.data());
console.log("assignby");
console.log(doc.id, " ===> ", doc.data());
this.goalList.push(doc.data());
});
});
// querySnapshot.forEach(doc=> {
// // doc.data() is never undefined for query doc snapshots
// console.log("assignby");
// console.log(doc.id, " ===> ", doc.data());
// this.goalList.push(doc.data());
// });
// });
});
}
【问题讨论】:
-
提醒:人称代词为大写字母“I”,无一例外。如果您难以记住这一点,请在浏览器中安装英文拼写检查器,并确保在发布前使用它。
-
人称代词是大写字母“I”....我已经注意到这一点:)
-
提醒:人称代词为大写字母“I”,无一例外。如果您难以记住这一点,请在浏览器中安装英文拼写检查器,并确保在发布前使用它。
标签: angular typescript firebase google-cloud-firestore