Issue
I need to store array of objects and then convert that into string using JSON.stringify and transfer that string to external link. Below is my typescript code..
let order_data = {};
this.storage.get("cart").then((cart) => {
cart.forEach((element, index) => {
orderItems.push({ "product_id": element.product.id, "quantity": element.qty });
});
});
console.log(orderItems);
order_data = {
"customer_name": "Singh",
"line_items": orderItems,
}
console.log(JSON.stringify(order_data));
In my console I got empty array of objects.....
{"customer_name":"Singh","line_items":[]}
Anyone here pls help me out of this... Thank you in advance....
Solution
You are using ES6 promise here. Promise works asynchronously. By the time your promise gets resolved, code below the promise is already got executed. You need to write that code inside .then()
block:
let order_data = {};
this.storage.get("cart").then((cart) => {
cart.forEach((element, index) => {
orderItems.push({ "product_id": element.product.id, "quantity": element.qty });
});
console.log(orderItems);
order_data = {
"customer_name": "Singh",
"line_items": orderItems,
}
console.log(JSON.stringify(order_data));
});
Answered By - Gourav Pokharkar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.