1

My data in firestore is arranged like this: enter image description here

enter image description here

What I want to archieve is basicly get all "values" out of each doc which I have 3 of this now in the one collection I show in the secound picture. I want to download it only once. The structure is the following:

chatrooms --> ChatId(1) --> ChatId(2) --> chatmessage

or simpler to understand:

collection --> doc --> collection --> doc

I want to loop through all chattmessages in "ChatId(2)" and get all "value"s out of each "chatmessage" and store them in an array.

I face the difficulty trying to handle such difficult (at least for me) nested calls to firebase and wanted to search for help here. Im working in react-native so Javascript is my language Im using here.

1 Answer 1

1

Got it working on my own, my code looks like this incase you want to replicate it:

const loadallmessages = async () => {
        await firebase.firestore().collection("chatrooms").doc(`${chatId}`).collection(`${chatId}`).orderBy("timestamp").limit(20).get().then(
            querySnapshot => {
                allChatMessages = [];
                querySnapshot.docs.map(doc => {
                    allChatMessages.push(doc.data().value);
                    //console.log("console log inside foreach: ", doc.data().value)
                })
                console.log(allChatMessages)
            })

    };

"Limit 20" is limiting my querysnapshot so I just receive 20 messages and because I order by "timestamp" I receive the 20 newest entries only. As a sidenote, the timestamp entry is something I do in my app itself using basic JS methods inorder to create later, who would have known, a timestamp to show in the chat. Everytime you send a message it gets attached to it and stored with the chatmessage itself. It goes like this:

const timestamp = Date.now()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.