简体   繁体   English

如何有效地从 Firebase 数据库中获取数组?

[英]How to get an array from Firebase database efficiently?

I want to get an array out of this:我想从中得到一个数组:

var db = firebase.firestore();
const first = db.collection('comments').orderBy('rating').limit(25);

Is there a function that "first" has that just lets me get the data out of the box?是否有一个 function 是“第一个”让我开箱即用的数据? I tried printing the object, but it's obfuscated or something;我尝试打印 object,但它被混淆了或者什么的; all the field names are random letters.所有字段名称都是随机字母。

Up to now, I've been iterating through using a Promise, but it's getting annoying quick.到目前为止,我一直在使用 Promise 进行迭代,但它很快就变得烦人了。

Up to now, I've been iterating through using a Promise, but it's getting annoying quick.到目前为止,我一直在使用 Promise 进行迭代,但它很快就变得烦人了。

You can't avoid using the promise returned by get() , as shown in the documentation .您无法避免使用get()返回的 promise,如文档中所示。 You will, at the very least, need to use await or then on it to wait for the async query to complete, then look at the QuerySnapshot it yielded by that promise.您至少需要使用awaitthen on it 来等待异步查询完成,然后查看promise生成的 QuerySnapshot。

If you want just the first item in the results set, you can simply use the first item in the docs array property of the QuerySnapshot.如果您只需要结果集中的第一项,您可以简单地使用 QuerySnapshot 的文档数组属性中的第一项。

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

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