golden_code
golden_code

Reputation: 49

pushing variables into a secondary child object in an array

I'm trying to add variables to existing JSON data received from an API when a user hits add button. When i try i get this error.

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

The data is assigned to a variable called pageList Below is how the data is returned from the server

DATA

[{"id":5,"referenceId":1189,"firstName":"Dan","lastName":"Daniels","orders":[{"id":109,"meals":[{"id":47,"name":"Fried Rice","description":"This is a  very sweet meal","image":"","mealType":"LUNCH","unitPrice":-20,"status":"ENABLED"}],"serveDate":"2019-07-11 00:00:00"}]}]

FUNCTION

AddtoList() {
       const meals =  {'id': '0','name': 'New Food', 'unitPrice':'5'}
       this.pageList.push({
                  'firstName': 'Jackson',
                  'lastName': 'Jack',
                  orders: {meals: meals}
                })
}

Upvotes: 0

Views: 41

Answers (2)

NCM
NCM

Reputation: 368

I think orders should be an array. Please see as below:

this.pageList.push({
  'firstName': 'Jackson',
   'lastName': 'Jack',
   orders: [{meals: meals}]
})

Upvotes: 1

Rahul Sharma
Rahul Sharma

Reputation: 10071

Update orders object to an array.

AddtoList() {
       const meals =  {'id': '0','name': 'New Food', 'unitPrice':'5'}
       this.pageList.push({
         'firstName': 'Jackson',
         'lastName': 'Jack',
         orders: [{meals: meals}]
        })
}

Upvotes: 1

Related Questions