This is currently what I have for Firebase v.8 but I need to understand how to implement it in V.9 so I can avoid receiving this error message:
TypeError: Cannot read properties of undefined (reading 'find')
function Sidebar() {
const [user] = useAuthState(auth);
const colRef = collection(db, 'chat');
const userChatRef = query(
colRef,
where('users', 'array-contains', user.email)
);
const chatsSnapshot = userChatRef;
const createChat = () => {
const input = prompt('Please enter an email address');
if (!input) return null;
if (
EmailValidator.validate(input) &&
!chatAlreadyExists(input) &&
input !== user.email
) {
setDoc(doc(collection(db, 'chats')), {
users: [user.email, input]
});
}
};
const chatAlreadyExists = (recipientEmail) => {
!!chatsSnapshot?.docs.find(
(chat) =>
chat.data().users.find((user) => user === recepientEmail)?.length > 0
);
};