简体   繁体   English

如何使用 JavaScript 从 Firestore 获取阵列?

[英]How to get an array from Firestore with JavaScript?

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.在我下面的应用程序中,我首先获取 4 个玩家的 uid,然后查询每个用户名和设备令牌 ID,它们分别存储在我的数据库中。

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.由于我是 JavaScript 的新手,我现在不知道如何从 Firestore 获取该数组并访问该数组中存储的数据。

I know how to get an array from Firestore with Android/Java but this seems not to work with JavaScript.我知道如何使用 Android/Java 从 Firestore 获取数组,但这似乎不适用于 JavaScript。

Any help is much appreciated!任何帮助深表感谢!

Here is my code that I use in a Cloud Function:这是我在 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:这是包含该数组的文档的集合:

在此处输入图像描述

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." queryResult是一个DocumentSnapshot :它“包含从 Firestore 数据库中的文档读取的数据。可以使用 .data() 或 .get() 提取数据以获取特定字段。”

As explained in the documentation here you can access the data in your code using this如文档here中所述,您可以使用此访问代码中的数据

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.我在互联网上搜索相同的问题,但信中我找到了 Firebase 阵列的绝佳替代品。

if you have problem with firebase array then如果您对 firebase 阵列有疑问,那么

not use Firebase Array use Firebase maps in firestore that's help you to work similar with vanilla JavaScript Array不使用 Firebase 数组 在 Firestore 中使用 Firebase 地图,这有助于您与香草 JavaScript 数组类似地工作

Visit Official Firebase Documentation 访问官方 Firebase 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM