I am trying to assign the variable "dish_data" to data that is returned from "dish_ref.get()," which returns a promise.
let dish_data;
var get_dish_data = dish_ref.get()
.then(doc => {
console.log('doc: ', doc);
console.log('doc: ', doc.data());
dish_data = doc.data();
console.log('dish_data in promise: ', dish_data);
});
console.log('dish_data after promise: ', dish_data);
const num_ratings = dish_data['num_ratings'];
"dish_ref" is a Google Firestore document. The console log for "doc" and "doc.data()" shows a firestore document and the dictionary data respectively. The console log also shows "dish_data" containing the information from "doc.data()" when within the promise but after the promise, "dish_data" is undefined.
I've tried creating an async function as follows...
async function myFunc() {
var get_dish_data = await dish_ref.get();
}
dish_ref.get().then(myFunc);
var dish_data = get_dish_data.data();
but I got a syntax error saying function was an unexpected token.