What is the best way to retrieve values from a JSON array object in an api?

How can I resolve the error I am encountering while trying to display array values from an API?

Error

Error displayed in console

The code snippets are provided below:

joblist.service.ts

import { Injectable } from '@angular/core';

import { HttpClient} from '@angular/common/http';

import { Observable } from 'rxjs';

import { IJobs } from '../jobs';


@Injectable({
  providedIn: 'root'
})
export class JoblistService {

 private _url: any = 'http://13.126.181.94:8087/joblisting.php?action=filter/';

constructor(private http: HttpClient) { }

getjoblists(): Observable<IJobs[]> {
return this.http.get<IJobs[]>(this._url);
}
}

joblist.component.ts

import { Component, OnInit } from '@angular/core';

import { JoblistService } from 'src/app/joblist/joblist.service';


@Component({

selector: 'app-joblist',

template: `
 <h2>Job Title</h2>
 <ul *ngFor='let joblist of joblists'>
 <li>
   {{joblist.job_title}} - {{joblist.job_id}} - {{joblist.job_add_date}} -
   {{joblist.date_diff}} - {{joblist.role}} - {{joblist.clientname}} -
   {{joblist.location}} - {{joblist.experience}} - {{joblist.salary}} -
   {{joblist.job_offer_type}} - {{joblist.companies_profiles_name}} - 
   {{joblist.job_description}} - {{joblist.skill_name}}
 </li>
 </ul>
  `,
 styles: []
 })


export class JoblistComponent implements OnInit {

public joblists = [];

constructor(public _joblistsService: JoblistService) { }

ngOnInit()
  {

 this._joblistsService. getjoblists()
 .subscribe(data => this.joblists = data);
   }
}

jobs.ts

Model for Jobs exported as IJobs

{

 job_title: string;

 job_id: number;

 job_add_date: Date;

 date_diff: number;

 role: string;

 clientname: string;

 location: string;

 experience: string;

 salary: string;

 job_offer_type: number;

 companies_profiles_name: string;

 job_description: string;

 skill_name: string;

}

In the console, the object is displayed as follows.

View image description here

Answer №1

Substitute -

<ul *ngFor='let joblist of joblists'>

with

 <ul *ngFor='let joblist of joblists?.all_search_view'>

When using *ngFor, make sure you are binding with an array and not an object.

Answer №2

Initially, within the joblist.service.ts

this.http.get<IJobs[]>(this._url);

Given that your response is not simply IJOBs[], you must specify the correct structure of the response body you are anticipating. This way, when you assign the value in component.ts, TypeScript will flag any errors automatically.

To resolve this issue, you have the option to adjust the structure as I mentioned earlier or follow the suggestion made by @Pardeep Jain. Both approaches will yield favorable results.

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

Integrate a service component into another service component by utilizing module exports

After diving into the nestjs docs and exploring hierarchical injection, I found myself struggling to properly implement it within my project. Currently, I have two crucial modules at play. AuthModule is responsible for importing the UserModule, which conta ...

Angular 10 - Compilation errors caused by the element's location

When running 'ng serve' or 'ng build' in Angular 10, I encountered a build error that stated: ERROR in projects/project-navigator/src/app/modals/building-permissions.component.html:89:61 - error NG8002: Can't bind to 'ngClass& ...

The mat-accordion is loading an incorrect component with a nested router configuration

Currently, I have implemented mat-accordion with router-outlet and noticed an unusual behavior. Below is a demonstration of the issue: https://stackblitz.com/edit/angular-material-accordion-with-router In this scenario, both panel 3 component and panel ...

Varieties of React components for arrays

Typically, when working with React functional components, we use React.FC<Props> as the return type. Take a look at this example component: const MyComponent = ({myArray}) => { return myArray.map( (item, index) => <span key={ind ...

Encountering a Type Error while using Typescript in conjunction with React-Redux

I've been experimenting with react-redux and typescript, but I encountered a type error when using connect() and mapStateToProps to inject props. Here's how my component is structured: function mapStateToProps(state) { return { coun ...

What could be causing the disappearance of the top and bottom lines in jquery datatable after the data details are

After setting up a screen with a jquery datatable that loads correctly, I noticed an issue. When I edit the details of a record and return to the table, only the row containing the table is visible. Here is my table declaration: $(document).ready(() => ...

Issue encountered while incorporating a dynamic field in Angular 5

My goal is to dynamically add a row of fields when the add button is clicked. While this functionality works fine, I encounter an issue where clicking the add button clears the values in the first row and adds a new row instead. Below is the code snippet I ...

Tips for implementing a reusable instance of a class in (SSR) React (comparable to a @Bean in Spring)

I am currently developing a new application using Next.js and React (Server Components) in TypeScript. In order to showcase and evaluate the app, I need to support two different data sources that provide identical data through separate APIs. My goal is to ...

I require the JSON data to be displayed in an array format

I am looking to convert a JSON object into a JSON array in Laravel. Currently, I am receiving JSON data in object format. $category = new Categories; return Response::json(array( 'error' => false, 'category' => $category- ...

Displaying Angular 4 HTTP GET response in HTML: A Step-by-Step Guide

I am struggling to retrieve data from a table using the HTTP GET method in Angular 4. How can I display this data on an HTML page? getData(){ //return this.data; return this.http.get("http://localhost:8080/SpringRestExample/get/filedata") ...

What is preventing TypeScript from identifying the type of a parent based on its child?

Take a moment to explore the following example: type Num = { type: 'NUMBER' numberValue: number } type Str = { type: 'STRING', stringValue: string } type B_Num = { a: Num; numberData: number; } type B_Str = { ...

What could be causing the ion-item click to malfunction in Ionic 4 on iOS devices?

ion-item clickable event is not working on iOS but it works fine on Android. I have been searching for a solution and have wasted a lot of time on it. Can someone please help me with this issue? <ion-content> <div> <ion-sear ...

Utilizing TypeScript to perform typing operations on subsets of unions

A TypeScript library is being developed by me for algebraic data types (or other names they may go by), and I am facing challenges with the more complex typing aspects. The functionality of the algebraic data types is as follows: // Creating ADT instatiat ...

Recursive function used to update an array of objects by dynamically mapping replies to comments

I have received a collection of comments from a graphql backend structured like this: [ { "__typename": "Comment", "id": "1", "userId": "1", "postId": "1", "parentCommentId": null, ...

Angular 10 - Timezone Detection and Interactive Calendar

I am encountering a major issue with the Angular DateTimePicker and TimeZone functionality. I need to dynamically switch the TimeZone at runtime in my component. Every date displayed on the frontend must be converted, while every date sent to the backend n ...

Creating a custom filter feature for an Angular 2 table that dynamically updates based on user input

I am currently working on adding a search feature to my table built using Angular Material. I have included an input field for Account ID and a dropdown for selecting the Department. I have used an HTTP get service to retrieve data from a JSON file and po ...

I'm curious if it's possible to superimpose a png image and specific coordinates onto a map by utilizing react-map

I am attempting to showcase a png graphic on a react-map-gl map, following the approach outlined here. Unfortunately, the image is not appearing as expected and no error messages are being generated for me to troubleshoot. Below is the snippet of code I&a ...

Guide to automatically have the md-select panel open when the page loads

Utilizing Angular4 along with Redux and angular material to create the layout of my website. In order to accomplish this, I am working on configuring the md-select panel to automatically open. For instance, when a button is clicked, it triggers an action ...

Managing the token prior to the page loading: best practices

Implementing JWT for authentication without a login page has posed some challenges for me. The issue arises when the site loads and immediately starts with the GET methods to load content before authenticating and saving the token in local storage. The pr ...

What is the best way to modify an API designed for the post method so it can be integrated with Google Sheets?

This is the code snippet: Example of using curl: curl -v -H "Authorization: Basic Y2xpZW50ZTpjbGllbnRl" -H "Content-Type: application/json" -d '{"cnpjRemetente":60701190000104,"cnpjDestinatario":30539356867,&qu ...