Transforming Json data into an Object using Angular 6

https://i.stack.imgur.com/JKUpL.png

This is the current format of data I am receiving from the server, but I would like it to be in the form of an Object.

public getOrder(): Observable < ORDERS > {
  return this._http.get < ORDERS > (`${this._apiBase}/charts/list/ORDERS/`);
}

This is how the data is currently being retrieved from the server.

ngOnInit() {
  this._dashService.getOrder().subscribe(order => {
    this.orders = order;
    console.log(this.orders);
  })
}

Answer №1

It is likely that your service is failing to define the Content-Type attribute in the http header as application/json.

If you have control of the service, consider adjusting the response's header property. If making modifications is not an option, attempting to use the JSON.parse() method may be helpful.

Answer №2

Here's the solution you need!

function retrieveOrders(): Observable < ORDERS > {
  return this._http.get(`${this._apiBase}/charts/list/ORDERS/`).map((response) => <ORDERS> response.json())
}

Answer №3

To easily handle your response, you can utilize the json method.

public retrieveOrderData(): Observable < ORDERS > {
  return this._http.get(`${this._apiBase}/charts/list/ORDERS/`)
                   .map((res) => res.results as ORDERS[] || [])
                   .catch((error:any) => Observable.throw(error.json().error));
}

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

Internet Explorer 10 not triggering the 'input' event when selecting an option from the datalist

Within this particular scenario, there is an input field paired with a corresponding datalist element. My aim is to develop JavaScript code that actively listens for when a user chooses an item from the list. Most resources suggest utilizing the "input" ev ...

Obtaining template attributes in CKEditor: A guide

I have been working with the template plugin in CKEditor to load predefined templates. Each template is defined as follows: templates: [ { title: "Quickclick 1", image: "template1.png", description: "Quickclick 1 template", html_et: "& ...

Select from a list to save

My goal is to create a feature where users can select a hotel name, number of days, guests, and peak time, the system will calculate them together and give a sum. Furthermore, I wish to store all user selections in the database, including the calculated to ...

JavaScript - Changing the position of an item within a JSON object

I have implemented JQuery sortable to rearrange items in a JSON object into a JSON array. Let's assume we have the following JSON file: [ { "ID_id": "I3Y0RAmsr5", "DT_createdAt": "2020-12-02T14:39 ...

Steps to efficiently enumerate the array of parameters in the NextJS router:

In my NextJS application, I have implemented a catch all route that uses the following code: import { useRouter} from 'next/router' This code snippet retrieves all the parameters from the URL path: const { params = [] } = router.query When I co ...

transferring information from a nested input field in Vue to its parent component

I need to send the input data from a child component to the parent component's data property by using the $emit method. Directly binding the transmitted property to the child property using v-bind within the <input v-model="userinput" /&g ...

Attempting to use jQuery AJAX to submit data without navigating away from the current webpage

Trying to implement the solution provided in this post where I am trying to send data to a PHP page and trigger an action, but unfortunately, it seems to just refresh the page without any visible outcome. Even after checking the network tab in the element ...

JavaScript - Declaring canvas as a global object

Presently, I am experimenting with HTML5 and using JavaScript to create a basic 2D Tile map. Progress is going smoothly; however, I have come across an issue where I am unable to contain everything within one large function. I am attempting to make the ca ...

"Upload a text file and create a JavaScript variable that contains the text within it

I am currently developing a library and I need to add a feature that reads the contents of a text file. Currently, it only returns the path. How can I modify it to return the actual content of the file? function displayFileContent() { var file = do ...

Passing Variables to Child Components with Vue Slots

Imagine creating a button component with a variable named myVar: MyButton.vue <template> <div> <slot :name="text"> My Button </slot> </div> </template> <script> export default { name: 'm ...

Utilizing query parameters in Next.js

I've been working on a unique Next.js application that incorporates both infinite scroll and a search input feature. The infinite scroll functionality loads 6 additional items whenever the user reaches the bottom of the page. On the other hand, the s ...

Encountered an issue in Laravel 5.7 JSONResource toArray method: Declaration must be compatible

I'm encountering an issue while trying to use the JSON Resource to Array converter in Laravel. Here is a snippet of my code: DataResource.php <?php namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate&bso ...

Issue encountered with Jquery Carousel: "Unable to access property 'safari' as it is undefined"

Having recently set up a duplicate of my website in a development directory, I've come across an issue with the jQuery Carousel not working properly. An error message now pops up saying: Uncaught TypeError: Cannot read property 'safari' of ...

My attempt to use the Redux method for displaying data in my testing environment has not yielded

I've been attempting to showcase the data in a Redux-friendly manner. When the advanced sports search button is clicked, a drawer opens up that should display historical data when the search attributes are selected. For this purpose, I implemented the ...

What could be causing my form to malfunction when attempting to submit data using Ajax and an external PHP script that is handling two string inputs?

Hello, I am facing an issue while trying to utilize Ajax to interact with a PHP file and submit both inputs (fullname and phonenumber). When I click the submit button, it simply refreshes the page without performing the desired action. Below is the code I ...

Tips for effectively combining an array with jQuery.val

My goal is to have multiple form fields on a page, gather the input results into an array, and then store them in a database. This process was successful for me initially. However, when I introduced an autocomplete function which retrieves suggestions from ...

Change a Character or Word in JavaScript after it's been typed

If I were to type the word "hello" into a textarea, is there a way for me to select that specific word and modify it afterwards? For instance, let's say I typed hello without hitting the space bar; could the system recognize this word and automaticall ...

Creating a Sudoku game board using underscore templates

Currently, I am in the process of constructing a Sudoku board using underscores templating. However, I have hit a roadblock when it comes to tackling the mathematical aspects necessary for deriving the table structure. My approach involves utilizing a 1d ...

Accessing JSON data in Python

I have been attempting to fetch weather predictions from DarkSky using their API by executing the following code. I am specifically interested in the hourly forecasted data: url="https://api.darksky.net/forecast/api_key/33.972386,-84.231986" response = re ...

Clone the children of an li element using jQuery, modify the text within a child tag, and then add it to

I have some awesome CSS that I want to recycle within a <ul>. My plan is to duplicate an existing <li> (to leverage the CSS), modify a <p> element, and then add it at the end of the <ul>. I believe I can achieve this by locating... ...