So currently I'm creating a website for learning purposes. I'm using SimpleCart for storing the items the user wants to purchase into localStorage. However, now I want to move the data off of localStorage and into my database. Currently I'm using Node.js to handle this. Here is a sample of the code I'm using to add it to my database.
function addToDBFood()
{
var OrderArray = JSON.parse(localStorage.getItem("simpleCart_items"));
var orderDetail = new Object();
for (var id in OrderArray) {
if (OrderArray.hasOwnProperty(id))
{
orderDetail.order_id = OrderArray.id;
orderDetail.order_item = OrderArray.name;
orderDetail.order_item_price = OrderArray.price;
orderDetail.order_quantity = OrderArray.quantity ;
var addToDB = new XMLHttpRequest();
addToDB.open("POST", '/orderdetail', true);
addToDB.setRequestHeader("Content-Type", "application/json");
addToDB.send(JSON.stringify(orderDetail));
}
}
}
Currently the problem I'm facing is that I cannot get the property of each individual object, for example, if I console.log(OrderArray.id)
I get undefined. The loop works, as far as I can tell because if I placed the console.log
in the loop, it will loop for however many objects I have inside localStorage and return that number of undefined in the console. My question is, how do I go about getting the the value of the property of each and every object?
EDIT: If I were to run JSON.parse(localStorage.getItem("simpleCart_items"))
it returns the following.