I have a class called user in which it has all the methods and instnace variables a user class would need. In it, i have a method which is responsible for returning query results to an index.js file. In this index.js file, i was hoping to set the state with the value from the query. But this does not work.
function collectres () {
var store ='';
var docRef = db.collection("cities").doc("SF");
docRef.get()
.then(function (doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
store = doc.data();// when referenced outside, it doesnt hold anything.
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
})
.catch(function (error) {
console.log("Error getting document:", error);
});
return store; // returns nothing and seems to not notice the assignment.
}
The function above was within my user class. When referenced from the index, it would look like this.
Random() {
let a = '';
a = user.collectres();
this.setState({name:a});
console.log(this.state.name);
}
however this would set the state with the previous value. When looking at the console log, i have noticed, the order of logging starts first with the index.js console.log(this.state.name),however shouldnt my collect res's log's show first. Any help would be appreciated.