嗨,我想看看聊天室是否已經存在。我不想再和同一個人聊天。我搜索了很多,我找到了這個代碼,但它對我不起作用。我可以和同一個人聊天。
像那樣的聊天室
firebase data
如果你能幫助我,我將非常感激。
const userChatRef = db
.collection('chats')
.where('users', 'array-contains', user.email);
const [chatsSnapshot] = useCollection(userChatRef);
const makeRoom = () => {
if (user.email !== email && !chatAlreadyExists(email)) {
db.collection('chats')
.add({
users: [user.email, email],
})
.then(doc => {
navigate(MESSAGEROOM, {id: doc.id, recipientEmail: email});
});
} else {
alert('Already exist!');
}
};
const chatAlreadyExists = recipientEmail => {
!!chatsSnapshot?.docs.find(
chat =>
chat.data().users.find(userEmail => userEmail === recipientEmail)?.length > 0,
);
不幸的是,在firebase查詢中,當前不能有多個
'array-contains'
語句。我建議只做以下幾點:我希望這在一開始會有所幫助,但請記住,當所有用戶都有大量聊天時,您將有大量的閱讀,因為在過濾掉之前,您必須獲取所有用戶的聊天記錄。