简体   繁体   English

我如何从 Firestore 文档中获取所有数据?

[英]How would I get all data from firestore document?

Before you tell me to read the docs , I have.在你告诉我阅读文档之前,我已经阅读了。 I've tried the examples on there and I haven't gotten it to work.我已经尝试过那里的例子,但我还没有让它工作。

I am simply trying to get all of the data from the muted section of my database and be able to check if they're still muted.我只是想从我的数据库的静音部分获取所有数据,并能够检查它们是否仍然处于静音状态。 (Basically I need to get their UserId, guild ID, mute start, and mute end data.) (基本上我需要获取他们的 UserId、公会 ID、静音开始和静音结束数据。)

From the docs I've tried:从我尝试过的文档中:

const mutedDB = db.collection('muted');
const queryRef = mutedDB.where('stillMuted', '==', true);
console.log(queryRef)

^ Returns ^ ^ 返回 ^ 在此处输入图片说明

and

const mutedDB = db.collection('muted');
const snapshot = mutedDB.where('stillMuted', '==', true).get();
if (snapshot.empty) return console.log('None')  
        
snapshot.forEach(doc => {
  console.log(doc.id, '=>', doc.data());
});

^ Returns an error ^ ^ 返回错误 ^

I'm unsure if I did those correctly, please let me know if there's a way to fix/solve this!我不确定我是否做对了,如果有办法解决/解决这个问题,请告诉我!

get() returns a promise that yields a DocumentSnapshot. get()返回一个产生 DocumentSnapshot 的承诺。 It does not itself return a DocumentSnapshot.它本身不返回 DocumentSnapshot。 You need to wait on the promise to fulfill in the usual JavaScript way:您需要等待承诺以通常的 JavaScript 方式实现:

const promise = mutedDB.where('stillMuted', '==', true).get();
promise.then(snapshot => {
    if (snapshot.empty) {
        console.log('None');
        return;
    }
    snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
    });
}

This is spelled out in the documentation that you linked, so I would still actually recommend that you go back and study the docs again.这在您链接的文档中有详细说明,因此我实际上仍然建议您返回并再次研究文档。

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

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