Javascript - sort documents in firestore, get data from them

I have a little problem regarding googles firestorage. I have data in my firestore sorted following way:

-chatrooms(collection)
    -chatroom1(document)
      -chatroom1(collection)
         -message1(document)
         -message2(document)
         -message3(document)
    -chatroom2
    -...

Every message is nothing else then a document formated the following way:

counter: integer
partner1: string
partner2: string
sender: string
value: string

My goal is to get the last chatmessage from each chatroom and store them in an array. I try following command for that:

lastmessage = await firebase.firestore().collection("chatrooms").doc(`${chatkeys[i]}`).collection(`${chatkeys[i]}`).orderBy("counter").limit(1).get().then(
                querySnapshot => {
                    querySnapshot.docs.forEach(doc => {
                        console.log(doc)
                    })
                }
            )

You see, I try to use orderBy and get the newest message out of all messages, also I limit it to 1 so I get only one result, the newest to be precise. After that I take the querySnapshot and want to pull out of the doc I get the "value" key (look above on how my message files are formated, they all have a "value" key, thats the chat message) but now I start to struggle.

As a result I get following code thats insanely long and I have no clue what is going on, I will show you a part of it:

"cP": e {
    "Qe": Map {
      "counter" => e {
        "Ne": 2,
        "Zt": 1,
      },
    },
    "ce": false,
    "converter": [Function anonymous],
    "hasCommittedMutations": false,
    "key": t {
      "path": e {
        "len": 4,
        "offset": 5,
        "segments": Array [
          "projects",
  ...(truncated to the first 10000 characters)

Can someone explain what is going on and even better, tell me how I can acomplish my goal? I would like to get only the "value" string out of the newest document. I fail to understand how I can use orderBy with getting data out of the filtered document.

Upvotes: 0

Views: 204

Answers (2)

John Doe
John Doe

Reputation: 1172

I think the following is a more appropriate solution here than the approved solution using forEach (meant to loop over an array of values) :

lastmessage = await firebase
  .firestore()
  .collection("chatrooms")
  .doc(`${chatkeys[i]}`)
  .collection(`${chatkeys[i]}`)
  .orderBy("counter")
  .limit(1)
  .get()
  .then(QuerySnapshot => 
    QuerySnapshot.docs[0].data()
  );

A QuerySnapshot contains zero or more DocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property

docs: Array<QueryDocumentSnapshot>

https://firebase.google.com/docs/reference/node/firebase.firestore.QuerySnapshot?authuser=0

Upvotes: 0

Donnald Cucharo
Donnald Cucharo

Reputation: 4126

As mentioned by @Aside in the comments, change how you log the output and use doc.data().value:

querySnapshot.docs.forEach(doc => {
  console.log(doc.data().value)
})

Here's an additional reference:

A QuerySnapshot contains zero or more DocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property or enumerated using the forEach method. The number of documents can be determined via the empty and size properties.

Upvotes: 1

Related Questions