I’m trying to build chat application using firestore and i want to get the latest value from array. How can i call it from cloud firebase.
<code> const unsub = onSnapshot(doc(db,'chats', userID), (doc)=>{
if(doc.data()){
setLastMessage( doc.data().messages )
}
})
return ()=>{
unsub()
}
</code>
<code> const unsub = onSnapshot(doc(db,'chats', userID), (doc)=>{
if(doc.data()){
setLastMessage( doc.data().messages )
}
})
return ()=>{
unsub()
}
</code>
const unsub = onSnapshot(doc(db,'chats', userID), (doc)=>{
if(doc.data()){
setLastMessage( doc.data().messages )
}
})
return ()=>{
unsub()
}
Currently i’m calling just an array. But i need to call the lastest value from it. Also the data in firestore looks like this:
<code>messages: [
0: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
},
1: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
}
]
</code>
<code>messages: [
0: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
},
1: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
}
]
</code>
messages: [
0: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
},
1: {
id: uuid(),
text,
senderPic: currentUser.photoURL,
senderId: currentUser.uid,
date: Timestamp.now(),
}
]