i want to read messagea from realtime database in firebase with vue or javascript in quasar framework
my collection name in db is messages that have this strtucture: ******************************************* my db structure*******
messages
-N4zbIt0Ey6Bix54f5SE
content: "Hi"
fromId: "nTTcnND2y6OHUBVO2GxHoBFwQf32"
timestamp: 1655703420767
toId: "0FXpksPdL4OAf9KAXx9B0R6nzmh2"
type: 1
-N4zbIt0Ey6Bix54f5SE
content: "Hi,HOW ARE YOU"
fromId: "0FXpksPdL4OAf9KAXx9B0R6nzmh2"
timestamp: 1655703448111
toId: "nTTcnND2y6OHUBVO2GxHoBFwQf32"
type: 1
do you know how i query firebase to getmessages?? ithis is for save in state with Vuex
firebaseGetMessages({ commit, state }, otherUserId) {
let userId = state.userDetails.userId
messagesRef = firebaseDb.ref('messages/' + fromId + '/' + toId)
messagesRef.on('child_added', snapshot => {
let messageDetails = snapshot.val()
let messageId = snapshot.key
commit('addMessage', {
messageId,
messageDetails
})
})
i think this code is wrong please help me if you have any experince with firebase
i am not familar with realtime-databse and query in fcm but familar with mongodb and sql server database i think that firebase has some function to read message!!! is it n't???
Your code indeed does not match the data structure that you shared.
Your code expects this structure:
messages: {
"uidOfSender": {
"uidOfRecipient": {
...
}
}
}
While your data structure has the UIDs of the sender and receiver as properties on each message in a flat list.
Worse: there is no way to get the messages between a sender and receiver from your current structure, as Firebase Realtime Database queries can only filter on one property. See my answer here for more on that: Query based on multiple where clauses in Firebase
The data structure in general looks like a 1:1 translation of what you'd do in a relation database, which is often not a great starting point when modeling data for a NoSQL database. What you'll want to do is create a different data structure where you set up a sort-of "chat room" for each pair of users, as I've shown here: Best way to manage Chat channels in Firebase
I also recommend reading NoSQL data modeling and watching Firebase for SQL developers.