简体   繁体   中英

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.)

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. It does not itself return a DocumentSnapshot. You need to wait on the promise to fulfill in the usual JavaScript way:

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.

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