I just want to check if i dont create an other chats on my firebase. I query all my chat with this email and then, i check if the array contain the email i wanna tchat with but it doesn't work. I'm using Firebase V9. Anyone has an idea ?
function Sidebar() { const [user] = useAuthState(auth);
const chatRef = collection(db, 'chats');
const qchatExist = query(chatRef, where('users', 'array-contains', user.email) );
const chatsSnapshot = getDocs(qchatExist);
const createChat = () => {
const input = prompt('Please enter a email adresse');
if(!input) return;
if (EmailValidator.validate(input) && !chatAlreadyExists(input) && input !== user.email ) {
console.log("valide");
addDoc(collection(db, 'chats'),
{
users: [user.email, input],
});
}
else {
console.log('invalide');
}
}
const chatAlreadyExists = (data) =>
!!chatsSnapshot?.docs.find(
chat =>
chat.data().users.find( (user) => user === data)?.length > 0
);