最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to get an array from Firestore with JavaScript? - Stack Overflow

programmeradmin3浏览0评论

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:

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:

Share Improve this question edited May 27, 2020 at 6:58 Renaud Tarnec 83.2k10 gold badges98 silver badges129 bronze badges asked May 27, 2020 at 6:10 KaiserKaiser 6069 silver badges24 bronze badges 3
  • The document you're showing doesn't seem to match the document you're querying. They have different paths. – Doug Stevenson 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 – Kaiser Commented May 27, 2020 at 6:24
  • can you show what you get on executing this line console.log(queryResult) – Shijil Narayanan Commented May 27, 2020 at 6:34
Add a ment  | 

3 Answers 3

Reset to default 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."

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());

});

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

发布评论

评论列表(0)

  1. 暂无评论