arihant-jain-09/discord-clone

Unhandled Rejection (TypeError): Cannot read property 'senderemail' of undefined

aniruddhac99 opened this issue ยท 3 comments

When did you faced this issue?

const channelRef=firestore.collection('servers').doc(currentserverid).collection('channels').doc(id).collection('messages');
const query=channelRef.orderBy('createdAt','desc').limit(1);
const [messages]=useCollectionData(query,{idField:'id'});

the above code gets the latest message, I have used react-firebase-hooks to get messages and then query to limit to 1.
You may see image link included for firestore database architecture..

2021-05-06 21_50_41-Clipboard

if(messages[0].senderemail===auth.currentUser.email){
     const messageRef=channelRef.doc(messages[0].id);
     await messageRef.update({
     message:messages[0].message+'\\n'+formValue
  },{merge:true})
}

I just cloned my repo and did not faced any error

I just cloned my repo and did not faced any error

Just fixed the issue. ๐Ÿ‘

Added an Optional Chaining Operator in line 56 of ChatSearchBar.jsx file, just after messages[0]

Before:

image

After:

image