Obtain JSON values using Angular's http get method and specify by ID

I need help displaying specific values from a JSON response using an interface:

interface:

export interface problemSer{
serviceProblem: string,
causes: any,
name: string,
solution: any
}

service.ts:

httpOptions = {
headers: new HttpHeaders({
  'Content-Type': 'application/json'
})
}

get(id: any): Observable<problemSer[]> {
return this.http.get<problemSer[]>(`${baseUrl}/${id}`, this.httpOptions)
}

component.ts:

services: problemSer[]=[];

ngOnInit(){ 
this.displayProblem("6154517d04dc7ac70253e2a8");
}

displayProblem(id: any){
this.problemService.get(id)
.subscribe((data: any) => {this.services = data.services;
  
  console.log(data);

});
}

Upon logging the data, the result will look like this:

{_id: '6154517d04dc7ac70253e2a8', serviceProblem: 'TESTESTEST', causes: Array(3), solutions: 
Array(2), createdAt: '2021-09-29T11:43:57.627Z', …}
causes: (3) [{…}, {…}, {…}]
createdAt: "2021-09-29T11:43:57.627Z"
serviceProblem: "TESTESTEST"
solutions: (2) ['22222', '222222']
__v: 0
_id: "6154517d04dc7ac70253e2a8"
[[Prototype]]: Object

However, I am facing difficulties in displaying individual values such as serviceProblem and solution when using ngFor loop.

HTML:

<ul>
  <li *ngFor="let problem of services"> {{problem.serviceProblem}} </li>
  <li></li>
</ul>

I aim to showcase single values like serviceProblem and solution only.

Answer №1

Spelling Mistake

this.problemService.fetch(id).subscribe((details: any) => {
  this.servicesList = details.services; // -> should actually be 'details'
  console.log(details);
});

Answer №2

Check the data in your console.log to ensure that the services object is present. If it is not there, review the output from your HTTP endpoint.

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

Encounter a problem while trying to access JSON data through HTTPGet on Android

My expertise lies in developing Android applications that can retrieve JSON data from a specified URL: I am currently working on displaying detailed information about vendors, and to achieve this, I pass the parameter "Lumineux" from my Android applicatio ...

Guide to displaying or concealing the header using PHP

Once the add button is clicked, the text should be hidden and the form should be displayed. Check out my code below: <h2 style="font-size: 2em; margin-bottom: 0.75em; margin-left: -1%;">Specify Co-Owners for self occupied property</h2> <div ...

Extract a continuous flow of decimal figures from a lengthy JSON string using Java

Note: I am seeking assistance in extracting a series of decimal numbers from a JSON string, with an interest in exploring the possibility of using regex for this task. Please consider this question unique and not a duplicate related to regex topics. The pr ...

Guide on setting default attributes for all properties of an object at once

Currently, I'm in the process of developing an AngularJS service provider (function) that achieves the following objectives: Gathers data from multiple tables within an SQLite database Delivers the resulting object to various controller functions S ...

Ways to Retrieve the Initial Object by its Index from the Specified Object Below

Is there a way to access FirstObj by index in TestCode[0].List? Any suggestions on the correct method to achieve this? 'TestCode':{ 'FirstObj':{ 'List':[ { year:2014 }, ...

When attempting to send data to the ServiceStack RESTful service, an error message of 'Access is denied' was received

I created a RESTful service using ServiceStack to send data to a database. It worked perfectly when tested locally. However, after deploying it to a server and running the same jQuery $.ajax call code, I encountered an 'Access is denied' error. I ...

What is the best way to display array outcomes with a jQuery conditional statement?

I am facing an issue while trying to retrieve store locations using conditional statements. Currently, when I select Dealer, Wholesale, or Retail from the options menu, the corresponding results are displayed. However, the problem arises when I select All ...

Angular CLI integrated with Isotope version 2

I am facing difficulties when using the isotope-layout module with Angular CLI. To install the module, I used the command: npm install isotope-layout --save After installation, I added the script in my .angular-cli.json file: "scripts": [ ... " ...

Sending data between Javascript and C# can be achieved by passing Javascript objects

After gathering information from a web form with Javascript on button_click, I have stored it in an object. Following this, I made an attempt to utilize Ajax and JSON to transmit the object to a method in my C# code. Javascript: $(document).ready(functio ...

Having trouble getting Laravel and Angular to filter data by categories?

I am currently developing an ecommerce project using Laravel and Angular. I have products and brands associated with these products. In my function to retrieve the products in Laravel, I have used a nullable parameter like this: public function index($bran ...

Using Vue.js and Vuex: What is the best way to bind a state value in Vue?

I'm attempting to connect the text within a b-dropdown element to a value stored in the store. I experimented with binding to a computed property because the value in the store is subject to change, and the b-dropdown's text needs to adjust accor ...

What is the process for incorporating links into Google GeoChart maps?

I'm currently working on developing a unique clickable map using Google's GeoChart for a website. My goal is to make each country clickable so that users can be directed to separate web pages when they select different countries on the map. Addit ...

Converting a JavaScript hash into a single object: A step-by-step guide

I have an array of objects in the following format: var log=[ { billkey:"Name", billvalue:"ABC" }, { billkey:"Department", billvalue:"Computer" }]; and I want to convert it into a single object like this: var log={ "Name":"ABC", " ...

Ways to efficiently incorporate data into App.vue from the constructor

My app initialization uses main.js in the following way, import App from './App.vue'; const store = { items: [{ todo: 'Clean Apartment.', },{ todo: 'Mow the lawn!', },{ todo: 'Pick up ...

Type Null cannot be assigned to the Element type

function displayList(items: ISharePointList[]) { let htmlContent = ''; items.forEach((item: ISharePointList) => { htmlContent += ` <ul class=""> <li><span class="ms-font-1">${item.ListTitle}</s ...

Tips for efficiently parsing and converting multiple JSON strings into dataframe rows using R

Greetings to the community, I am a newcomer to stackoverflow and I hope I have presented my problem accurately. I have an R dataframe structured as follows: ID|Product1|Type |Product_JSON_string 1 | Bread |Grocery |{"ID":"1",&quo ...

JavaScript in an array of arrays consists of different arrays

I've been searching for a solution to this issue, but haven't been able to find one. If I have an array of arrays, is there a way to check if one of those arrays is included in the main array? While I tried using Array.prototype.includes(), it d ...

Tips for incorporating CSS classes into a React component

Currently, I am iterating through an array of data to create a list. My goal is to add a CSS class when a specific list item is clicked. Using Iteration for List Creation let sportsList = allSports.sportList.map((sport) => { return ( <SportItem i ...

React form submissions are not saving the state

I currently have dynamic components rendered from the server side, including a submit button component. The issue I am facing is that when I submit the form, the state reverts to its initial values instead of retaining the updated values set by child compo ...

Passing data through events from a parent component to a child component in Vue.js using the $emit method

Having trouble making this work! I have included the $emit code in my click event to trigger a custom event. <h2 class="form_section--header">Business Hours - <a href="javascript:void(0)" v-on:click="$emit('addBusinessHours', null)"> ...