Reputation: 2715
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.
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.
ADD STATE
class Fndate extends React.Component {
state = {
options: [],
anzic: [],
country: [],
region: [],
}
selectCountry (val) {
this.setState({ country: val });
}
selectRegion (val) {
this.setState({ region: val });
}
async componentDidMount() {
// const fsDB = firebase.firestore(); // Don't worry about this line if it comes from your config.
let options = [];
await fsDB.collection("abs_for_codes").get().then(function (querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, ' => ', doc.data());
options.push({
value: doc.data().title.replace(/( )/g, ''),
label: doc.data().title + ' - ABS ' + doc.id
});
});
});
let anzic = [];
await fsDB.collection("anzic_codes").get().then(function (querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, ' => ', doc.data());
anzic.push({
value: doc.data().Title.replace(/( )/g, ''),
label: doc.data().Title + ' - ANZIC ' + doc.id
});
});
});
await fsDB
.collection("newsletter")
.get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data().email}`);
}
);
});
this.setState({
options,
anzic
});
}
Upvotes: 2
Views: 7226
Reputation: 319
You can use an automated script for this. All you need is python and firebase_admin.
Upvotes: -1
Reputation: 3406
I believe you're using React, and I just realized that you're calling the method in question in the render
method.
try doing it in componentDidMount
. A reference can be found here
so your code would be like this:
class YourClass extends React.Component {
componentDidMount(){
fsDB.collection("newsletters").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
});
});
}
render(){
return (....your existing jsx....)
}
}
Note that I don't know your class name or how you define your component. But I'm just saying that you need to move your code in the componentDidMount
.
We couldn't see your entire file. But I'm sure that it's a syntax error (especially with JSX)
Upvotes: 2
Reputation: 58
I think you should get rid of ‘.query’.
and the .empty
returns false
if you have data in the collection.
but i wonder, why is querySnapshot
undefined in your case.
db.collection("lessons").get()
.then(function(querySnapshot) {
if (!querySnapshot.empty) {
querySnapshot.docs.map(function(documentSnapshot) {
console.log(documentSnapshot.data().name);
})
} else {
console.log('no documents found');
}
});
console
Upvotes: 0