Reputation: 9
I have some JSON which looks like this.
[
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
},
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
}
]
It's being passed to me as 'newItem'. And is being set twice in the existing code I'm working on:
that.cart.addItem(newItem.toObject());
And later:
that.origCart.replaceWith(that.cart);
This traditionally has worked fine when 'items' was a flat list. However, the JSON above is the new JSON format and therefor I need to pass 'items' for each category/name.
Inside the 'for' loop I have this outputting what I need for each.(I need to keep them separated.)
newItem.items
However, when I attempt to perform the following I get a Type Error. (Yes, I'm going to have multiple 'carts' once I get this working.)
that.cart.addItem(newItem.items.toObject());
TypeError: newItem.items.toObject is not a function
What can I do to get 'newItem.items' to the proper type, or what operation can I substitute for to get these set properly? I tried a bunch of things I found online without success and now I'm turning to the experts. Just not my forte (yet).
Upvotes: 0
Views: 115
Reputation: 804
It looks like you're trying to call an items function from the newItems object even though items is an Array. Try the following:
that.cart.addItem(newItem.items);
Upvotes: 1