I have a for loop that creates products in the database and also want that productsarray to be pushed in another collection ,so I want it to store in a temporary array,but before executing the complete for loop the javascript control goes and executes the rest of the code due to which the temporary array is empty. I want something so that the javascript control goes to next statement only after the for loop is executed completely.
for(let i=0;i<productsname.length;i++){
name=productsname[i]
cost=productcost[i]
Product.create({name:name,cost:cost},function(err,product){
productArray.push(product);
})
}
var newOrder={id:count++,dateofdispatch:req.body.date,transport:req.body.transport,amount:req.body.totalcost,product:productArray}