Friday, 31 May 2019

How do you read data from firestore?

I'm trying to learn how to use firestore.

The google documentation says to try this format:

db.collection("users").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });
});

So, I used that to do this (changing the db to fsDB and "users" to "newsletters"

fsDB.collection("newsletters").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });
});

When I try this, I get an error that says querySnapshot is not defined. Screen shot of error message is attached.

enter image description here

I've seen this post and tried its suggestion as follows:

fsDB.collection("newsletters").query.get().then(function(querySnapshot) { 

                            if (querySnapshot.empty) { 
                                console.log('no documents found');
                            } else {
                                    querySnapshot.docs.map(function (documentSnapshot) {
                                        console.log(documentSnapshot.data().name); 
                                    });
                            }

                        });

This attempt generates a parsing error with the if statement, but I can't find a way to solve that either.

This must have a simple solution, but I can't figure out how to get a record from the database.

NEXT ATTEMPT

I tried moving the code above the return statement and putting in inside a variable as follows shown in the attached screen shot, but it still produces errors as shown. These errors are the same regardless of whether i keep or lose the brackets around querySnapshot.

enter image description here5]5



from How do you read data from firestore?

No comments:

Post a Comment