Here is the code and when I run this it executes only One Time ...
export const postStock = (body) => {
for (let val of body.order.values()) {
let sql = ` INSERT INTO stockmaster (stocknum, cat_id, user_id, dyenumber, stockQty) VALUES ('${body.stocknum}', ${JSON.stringify(val.cat_id)}, '${body.user_id}', ${JSON.stringify(val.dyenumber)}, ${JSON.stringify(val.stockQty)})`;
return sql;
};
};
So how can I run for loop or prevent it from stopping it ??
CodePudding user response:
return
stops execution and exits the function. return
always exits its function immediately, with no further execution if it's inside a for loop.
So you should avoid return
in case you want the loop to continue. What you can do is store the sql into array or print the data without returning anything
CodePudding user response:
You can use this. return makes the loop function to exist.
export const postStock = (body) => {
let values = body.order.values();
let sql = values.map((val) => `("${body.stocknum}",
"${JSON.stringify(val.cat_id)}", "${body.user_id}",
"${JSON.stringify(val.dyenumber)}", "${JSON.stringify(val.stockQty)}")`)
let query = "INSERT INTO stockmaster (stocknum, cat_id, user_id, dyenumber,
stockQty) VALUES " sql
}