0

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???

Mostafa Asadi
  • 17
  • 1
  • 4

1 Answers1

0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thank you mr.frank for your answer. you answer my question exactly becaus that javascript code read message from firebase with that structure point it. may be to write javascript code for my Message collection with structure that point in question? – Mostafa Asadi Jun 28 '22 at 09:22
  • and very thanks for your firebase for sql developers link. – Mostafa Asadi Jun 28 '22 at 09:43
  • If the explanation of why your current code doesn't work and the structure for a chat app in my answer are clear, the next step is for **you** to write code to implement that for your ap. If you get stuck while doing so, I recommend posting a new question showing what you tried already based on the new insights you got from this answer. – Frank van Puffelen Jun 28 '22 at 16:55