gpt4 book ai didi

javascript - Firestore 抛出 'onSnapshot() requires between 1 and 4 arguments'

转载 作者:搜寻专家 更新时间:2023-10-30 22:45:32 25 4
gpt4 key购买 nike

我正在尝试从 firebase 获取所有当前用户未读的消息。问题是我的 onSnapshot() 返回了以下错误,但它在初始加载时返回了我需要的值。如果添加了新文档,则 onSnapshot() 不会因为该错误而再次触发

FirebaseError: Function Query.onSnapshot() requires between 1 and 4arguments, but was called with 0 arguments.

这是接收所有当前用户未读消息的辅助函数。

async getUnseenMessagesCount() {
const collectionRef = (await firestore()).collection(this.collectionPath) //chats/${user_id+second_identifier/messages}
let allMessagesCount = 0
let currentUserReadMessagesCount = 0
try {
collectionRef.onSnapshot().then(snapshot => {
allMessagesCount = snapshot.docs.length
})
collectionRef
.where('seenBy', '==', '') // compare against empty string because seenBy is userId.
.onSnapshot()
.then(snapshot => {
currentUserReadMessagesCount = snapshot.docs.length
})
} catch (error) {
console.log(error)
}

console.log(allMessagesCount)
console.log(currentUserReadMessagesCount)
console.log(allMessagesCount - currentUserReadMessagesCount)
}

因为我想从用户参与的所有聊天中获取所有未读消息计数,所以我在我的 vuex 操作中执行以下操作,该操作在身份验证状态更改时激活:

new UserChatsDB(newUser.id).readAll().then(snapshot => { //users/id/chats/{chat_id: user_id+second_identifier}
if (snapshot.length > 0) {
snapshot.forEach(element => {
console.log(element)
const count = new MessagesDB(
element.chat_id
).getUnseenMessagesCount()
console.log(count) //Returns pending Promise
})
}
})

什么会导致上述错误?有更好的方法吗?让我知道是否需要数据库结构。非常感谢任何帮助!

最佳答案

根据docs对于 firebase,您必须像这样使用 onSnapshot() 函数:

async getUnseenMessagesCount() {
const collectionRef = (await firestore()).collection(this.collectionPath) //chats/${user_id+second_identifier/messages}
let allMessagesCount = 0
let currentUserReadMessagesCount = 0
try {
collectionRef.onSnapshot(snapshot => {
allMessagesCount = snapshot.docs.length
})
collectionRef
.where('seenBy', '==', '') // compare against empty string because seenBy is userId.
.onSnapshot(snapshot => {
currentUserReadMessagesCount = snapshot.docs.length
})
} catch (error) {
console.log(error)
}

console.log(allMessagesCount)
console.log(currentUserReadMessagesCount)
console.log(allMessagesCount - currentUserReadMessagesCount)
}

所以你必须删除你的then()

关于javascript - Firestore 抛出 'onSnapshot() requires between 1 and 4 arguments',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57903018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com