1

In my below app I first get the uids of 4 players and then make a query for each username and the devices token id, that are stored separately in my database.

To save reads I stored both informations now in the same document in an array. Since I am a novice in JavaScript I don't know how to get this array now from Firestore and access the stored data in that array.

I know how to get an array from Firestore with Android/Java but this seems not to work with JavaScript.

Any help is much appreciated!

Here is my code that I use in a Cloud Function:


...

// This is where the array is stored, I now need to get the array and access the data of it
      admin.firestore().collection("User").doc(uid_player_1).collection("User Info").doc("UsernameToken").get().then(queryResult =>{
        // You need to get the array that is stored there. The first value (0) there is the username, the second is the device token

console.log(queryResult)

      });

...

This is the log I get:


QueryDocumentSnapshot {
  _fieldsProto: 
   { usernameToken: { arrayValue: [Object], valueType: 'arrayValue' } },

...

Here is the collection with the document that contains the array:

enter image description here

3
  • The document you're showing doesn't seem to match the document you're querying. They have different paths. Commented May 27, 2020 at 6:15
  • @DougStevenson I edited my post. Now you see the document I am querying that contains the array in the image Commented May 27, 2020 at 6:24
  • can you show what you get on executing this line console.log(queryResult) Commented May 27, 2020 at 6:34

3 Answers 3

6

If I correctly understand your question, the following should do the trick:

      admin.firestore().collection("User").doc(uid_player_1).collection("User Info").doc("UsernameToken").get().then(queryResult =>{

          const username = queryResult.data().usernameToken[0];
          const token = queryResult.data().usernameToken[1];

      });

queryResult is a DocumentSnapshot: It "contains data read from a document in your Firestore database. The data can be extracted with .data() or .get() to get a specific field."

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that was exactly what I was looking for. I didnt know how to access the data within an array but this worked, thank you
You are welcome. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… for more details on Arrays.
2

As explained in the documentation here you can access the data in your code using this

admin.firestore().collection("User").doc(uid_player_1).collection("User Info").doc("UsernameToken").get().then(queryResult =>{

 console.log(queryResult.data());

});

Comments

0

i search on the internet for the same question but letter i found a great alternative of Firebase Array.

if you have problem with firebase array then

not use Firebase Array use Firebase maps in firestore that's help you to work similar with vanilla JavaScript Array

Visit Official Firebase Documentation

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.