storing data in a nested child object within an array

I am attempting to include variables into the existing JSON data that is received from an API whenever a user clicks on the add button. However, I encountered this error message:

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

The data is stored in a variable called pageList. Below is an example of 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}
                })
}

Answer №1

In my opinion, it would be beneficial if orders is structured as an array. Consider the following example:

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

Answer №2

Convert the orders object to an array in the update process.

AddToList() {
       const meals =  {'id': '1','name': 'Fresh Meal', 'price':'6'}
       this.listItems.push({
         'firstName': 'Olivia',
         'lastName': 'Owen',
         items: [{meal: meals}]
        })
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying a PDF in a new browser tab using JavaScript after retrieving data with cURL

Seeking guidance here. I currently have a URL for Phantomjs that produces a PDF, but my goal is to generate the PDF on the server side. <script> $("#generatePDF").click(function(){ var fullLink = "<? echo $link ?>" $.ajax({ ...

Material UI AppBar displaying custom colors instead of theme colors

Currently, I am utilizing Material UI version 4.7.0. In my project, I have established a customized theme (shown below) by utilizing createMuiTheme() method with personalized primary and secondary colors set. Within my AppBar component (also displayed be ...

Enhancing the user experience by providing auto-complete suggestions while still maintaining the original form functionality

Encountering a curious bug that appears to be affecting only IE and Webkit browsers. I've set up a simple form as follows: <div id="output">Form output is shown here</div> <form id="myForm" action="#" method="post"> <input type= ...

Guide on adding HTML to a specific div element in jQuery by utilizing the keyword "(this)"

I have been working on a script for my PHP page in Wordpress to add titles to images in my photo gallery that open in a lightbox. Here's what I developed: $(".responsive1").bind("click", function() { $(this).("<div class='imgTitle ...

Is there a way to refresh CSS styles when the window width is adjusted?

Is there a way to refresh CSS styles when the window width changes? I attempted this method, but unfortunately it did not work as expected. A simple refresh (F5) helps to rectify the CSS tags. jQuery(function($){ var windowWidth = $(window).width(); ...

Tips for triggering functions when a user closes the browser or tab in Angular 9

I've exhausted all my research efforts in trying to find a solution that actually works. The problem I am facing is getting two methods from two different services to run when the browser or tab is closed. I attempted using the fetch API, which worke ...

Utilizing PrimeNG dropdowns within various p-tree nodes: Distinguishing between selected choices

I am working with a PrimeNg p-tree component <p-tree [value]="treeNodes" selectionMode="single" [(selection)]="selectedNode"></p-tree> The treeNodes are defined using ng-templates, with one template looking li ...

Ways to retrieve and bind data using onMounted in VueJS

Loading Data in Test.vue Component <template> <li v-for="item in masterCompany" v-bind:key="item.id"> {{ item.displayName }} </li> </template> <script> import Test from "../hooks/Test.hook" ...

Difficulty loading AJAX with autocomplete feature. Any suggestions?

I have created a jQuery autocomplete feature that works correctly, but when the value is removed using the Backspace key, the 'LOADING...' message remains visible instead of hiding. How can I make it so that after removing the value with the Back ...

Utilizing Node.js to Retrieve a POST Request JSON and Modify its Format

Received an incoming Post request in JSON format like this: [{"username":"ali","hair_color":"brown","height":1.2},{"username":"marc","hair_color":"blue","height":1.4},{"username":"zehua","hair_color":"black","height":1.8}] Need to transform it into the f ...

Issue: NextRouter is not mounted when the client is used

After implementing use client, I encountered the following error: Error: NextRouter was not mounted error. However, upon removing use client, a different error surfaced: Error when use client is removed: ReactServerComponentsError: A component requirin ...

How to eliminate blinking in Ajax refresh upon successful completion

I have come across this issue, and although I've checked out a solution on Stack Overflow (ajax refresh - how to remove the blinking), it hasn't resolved my problem. Is there a way to prevent the page from blinking every 3 seconds when the Ajax ...

Looking for assistance in correctly identifying types in react-leaflet for TypeScript?

Embarking on my 'first' project involving react-scripts-ts and react-leaflet. I am attempting to create a class that should be fairly straightforward: import {map, TileLayer, Popup, Marker } from 'react-leaflet'; class LeafletMap exte ...

Having trouble accessing an injector service within the promise of a dynamically loaded JavaScript function that has been assigned to a global variable

Query I am facing an issue while trying to integrate PayPal with Angular. I am encountering difficulties when attempting to call an injected service inside a function of the promise returned. Any assistance in resolving this would be greatly appreciated. ...

Hover over to reveal sliding menu

Hey everyone! Take a look at this code snippet: <body> <div id="slidemenubar"> <a href="">1</a> </div><br> <div id="slidemenubar2"> <a href="">2</a> </div> </body> When hovering over the ...

Would it be possible to continuously send multiple documents to MongoDB using a loop?

I'm facing difficulties in transmitting sensor data to MongoDB using JavaScript. Although I came across options like MongoDB Atlas, I am searching for a more straightforward way to accomplish this. Below is my code: const db = client.db(databaseName) ...

EJS: Dynamically linking CSS and JS files according to specific page conditions

Is there a way to conditionally call CSS/JS files based on specific page conditions in EJS? Can we use a flag from the router or base it on the URL in the EJS file? Note: The code below works perfectly when accessing the /editor page, but it will cause er ...

Tips on concealing Bootstrap 5 modal through button press

I've created a Bootstrap 5 modal that allows users to download a file by clicking a button. Everything works as expected, but I'm facing an issue with hiding the modal after the download button is clicked. I've been trying to use jQuery to ...

Dealing with Errors in Angular 2/4 using forkJoin for Multiple URLs

I am currently using Angular 2 and implementing forkJoin for a particular scenario where I need to make multiple REST calls in parallel. Below is the function I am using: private getBatchObservableData(data: Array<Widget>): Observable<any> { ...

Transform an Angular 4 application into a desktop application by utilizing Electron

After creating an application with Angular 4, I am looking to develop a desktop version. My research has pointed me towards using Electron as the best solution for this task. Can anyone provide guidance on how to convert my Angular 4 application into an El ...