Closed MZ071999 closed 2 years ago
@chrisbianca would appreciate if you could take a look at this
@MZ071999 this is almost certainly an issue with the way you have structured your code and not the useCollection
hook itself. I'm happy to investigate if you can bring a reproducible example demonstrating that useCollection
is causing the problem, but otherwise this is a question more suited for Stack Overflow.
@MZ071999 this is almost certainly an issue with the way you have structured your code and not the
useCollection
hook itself. I'm happy to investigate if you can bring a reproducible example demonstrating thatuseCollection
is causing the problem, but otherwise this is a question more suited for Stack Overflow.
Thank you
I believe it is causing the problem because:
useCollection
hook always returns empty valueuserChatRef
, I did get the ref value and the connection to firebase are all definedQuerySnapShot
returned by useCollection
, I got an empty array when it should have returned an array with valuesI use the same hook in another component to retrieve the recipient's data but it also doesn't work
function Sidebar(props) {
const userEmail = props.props[1];
const { data: session } = useSession();
const userChatRef = dbs
.collection("chats")
.where("users", "array-contains", userEmail);
const [chatSnapshot] = useCollection(userChatRef);
useEffect(() => {
if (session) {
dbs.collection("users").doc(userEmail).set(
{
email: userEmail,
lastSeen: firebase.firestore.FieldValue.serverTimestamp(),
},
{ merge: true }
);
}
}, []);
return (
<div className={classes.container}>
<Header />
{chatSnapshot?.docs.map((chat) => (
<Recepeint key={chat.id} id={chat.id} users={chat.data().users} />
))}
</div>
);
}
i console log chatSnapShot
and this is what I got:
the array returned is empty. This problem only happens in production, everything is working in development mode.
@MZ071999 this is almost certainly an issue with the way you have structured your code and not the
useCollection
hook itself. I'm happy to investigate if you can bring a reproducible example demonstrating thatuseCollection
is causing the problem, but otherwise this is a question more suited for Stack Overflow.
Update: the useCollection hook returns value for a very brief second then disappear
@MZ071999 have you checked in the firebase console that the value actually exists? It sounds to me like your writes are being rejected by firestore
@MZ071999 have you checked in the firebase console that the value actually exists? It sounds to me like your writes are being rejected by firestore
yes the value exists, I managed to receive them in development mode.
I have set the rules to allow read and write, I don't think firestore is rejecting the writes because I managed to create a new chat and I can see the value in firestore. It's just the useCollection hook is returning an empty array. I'm still not sure what's going on
update:
I tried the code with vanilla useEffect
and onsnapshot
and I still got an empty array so now I believe the problem is not with useCollection() but probably with the read configuration. I can write but cannot read, that's what I'm getting now
I have a next.js application in which there is a real-time chat feature.
I'm using the
useCollection()
hook to retrieve the chat data from firebase.So the mechanism that I'm trying to achieve is:
PROBLEM:
When "send message" is clicked,
createChat()
kicks in, butchatAlreadyExist
doesn't work. So firebase always creates a new chat between the two users even though the chat has already exist.I don't think there's any problem with the firebase connection because a new chat is created every time
createChat()
is triggered. I believe something is wrong with theuseCollection
hook but I'm not sure what and how to fix it.NOTE:
Everything is working as intended in the DEVELOPMENT mode. It is only after deploying to vercel the
useCollection
hook doesn't seem to work anymore.This is the code: