简体   繁体   中英

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? I tried printing the object, but it's obfuscated or something; all the field names are random letters.

Up to now, I've been iterating through using a Promise, but it's getting annoying quick.

Up to now, I've been iterating through using a Promise, but it's getting annoying quick.

You can't avoid using the promise returned by get() , as shown in the documentation . 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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