Wednesday, 4 December 2019

Making a callback function run before the outer function

Hi,So I'm having a weird issue with my node js code (sorry if it is bad, I have no experience with this)Pizza.all((err, rows) => {let pizzas = [];rows.forEach((row)=>{let pizza = {"pizzaId": "", "name": "", "price" : 0.0, "size": "", "crust": "", "sauce": "", "extraCheese": "", "ingredients": {}};pizza.pizzaId = row.pizzaId;u/ = row.nameu/.price = row.price;pizza.size = row.size;pizza.crust = row.crust;pizza.sauce = row.sauce;pizza.extraCheese = row.extraCheese;pizza.ingredients[pizza.pizzaId] = [];PizzaToppings.find(pizza.pizzaId, (err, rows) =>{rows.forEach((row) =>{pizza.ingredients[pizza.pizzaId].push(row);console.log(pizza.ingredients["P1"]);});});console.log(pizza.ingredients["P1"]);pizzas.push(pizza);});​My inner function is running first instead of my outer one, so my ingredients list is not being populated outside, but is being populated when it is inside the callback function.Will Promises help solve this problem or is something else supposed to be done?​Thanks!

Submitted December 04, 2019 at 04:25PM by indisapien

No comments:

Post a Comment