VueJs,Vuefire,Firestore:如何筛选同步的Firestore文档



使用vuefire可以在vue实例上设置firestore对象,并自动将其与firestore同步。我还想为另一个数据点过滤,但过滤会导致错误。

下面的代码返回所需的工作人员,并且工作正常。Firebase用户(fbUser(返回

WEBPACK_IMPORTED_MODULE_0__services_Firebase.acollection(…(.filter不是的功能

并且VueTools 中的数据点为空

firestore() {
return {
staff: db.collection('staff').orderBy('lastname'),
fbUser: db.collection('staff').filter(s => s.email === this.current_user.email)
}
},

db.collection不会直接返回结果。我不知道实现,但我想vuefire会创建一个名为fbUser的数据字段,并将结果保存在其中

我认为,最好的解决方案是使用计算数据,如:

computed: {
fbUserFiltered () {
return this.fbUser.filter(s => s.email === this.current_user.email);
}
}

我意识到这是一个老问题,但我也有同样的问题,想要一个更一般的答案

如果您想避免查询整个集合而在客户端上丢弃不匹配的结果,您可以在created()事件中使用$bind()函数,例如

data: () => ({
finance_staff: [],
}),
created() {
const filtered_entries = db.collection('staff').where('department', '==', 'finance');
this.$bind('finance_staff', filtered_entries);
},

或者,如果您需要等待身份验证状态:

created() {
auth.onAuthStateChanged(async user => {
if (!user) return;
const filtered_entries = db.collection('staff').where('manager_id', '==', user.uid);
this.$bind('my_staff', filtered_entries);
});
},

请注意,如果您已经知道文档ID,那么它会更简单:

firestore: {
current_employee: db.collection("staff").doc("abcd1234")
},

最新更新