【发布时间】:2019-08-07 00:24:51
【问题描述】:
我有用户...每个用户都有多个他/她所属的组。每个用户组成员身份都是嵌入在他/她的用户文档中的数组。有一个任务集合,每个任务包含一组authorizedGroups,对应于用户的组。大多数任务只有一个授权组,但有些任务有几个组。任务由一个用户分配给另一个用户。
我想查询用户可用的所有任务,并为每个查询设置监听器。
使用 get() 已经成功,但没有提供我希望的活动侦听器。有没有一种方法可以为每个组生成监听器? onSnapshot 方法不返回承诺。不清楚我如何才能实现这个目标。任何建议将不胜感激。
created() {
const getTasksPerGroup = async (groups) => {
// Make a get request using each group in users group membership list
const queries = groups.map((group) => {
return this.$store.state.db
.collection('tasks')
.where('authorizedGroups', 'array-contains', group)
.where('taskAssignedTo', '==', this.$store.state.auth.user.displayName)
.get()
})
// Use Await Promise.all to acquire an array of querySnapshots
const result = await Promise.all(queries).then((querySnapshot) => {
return querySnapshot.map((querySnapshot) => querySnapshot.docs).reduce((acc, docs) => [...acc, ...docs])})
//Result is an array of querySnapshots...pushing each qs.data() to the tasks array
let tasks = []
result.forEach((qs) => {
let item = qs.data()
tasks.push(item)
console.log('tasks:', tasks)
this.$store.commit('tasks/mutate_tasksArray', tasks)
})
}
// Invoke async function that gets the user's groups array
getTasksPerGroup(this.allFirmGroups)
}
【问题讨论】:
标签: javascript vue.js google-cloud-firestore