I'm learning about Cloud Firestore (coding on Nodejs/Express - restfull api). I'm having trouble with async and await. According to the code below, the order of appearance is Step B, Step A. So the output value of restaurants is empty. How to make the order appear as Step A, Step B
Thank you.
async function getRestaurants_Detail(snapshot, res) {
var restaurants = [];
snapshot.forEach( async (doc) => {
let newRestaurant = await doc.data();
var cuisines = [];
if (newRestaurant.cuisineListRef) {
for (var i = 0; i < newRestaurant.cuisineListRef.length; i++) {
const snapShotCuisine =await newRestaurant.cuisineListRef[i].get();
const title = snapShotCuisine.data().title;
cuisines.push(snapShotCuisine.data());
}
}
restaurants.push({
id: doc.id,
title: newRestaurant.title,
content: newRestaurant.content,
address: newRestaurant.address,
cuisines,
});
debugApp("Step A");
debugApp(restaurants);
})
debugApp("Step B");
debugApp(restaurants);
return ApiSuccess.send(res, { restaurants: restaurants });
}
async getRestaurants(req, res, next) {
try {
const restaurantsRef = db.collection("restaurants");
const snapshot = await restaurantsRef.get();
return !snapshot.empty ? getRestaurants_Detail(snapshot,res) : ApiSuccess.send(res, { restaurants: [] });
} catch (err) {
console.log(err);
return next(
ApiError.internal_500(
"The restaurants you were looking for do not exist",
err
)
);
}
}