I am trying to get the results
field from a json response however I get the error Property 'results' does not exist on type 'AppComponent
import { Component } from '@angular/core';
//in place where you wanted to use `HttpClient`
import { HttpClient } from '@angular/mon/http';
@Component({
selector: 'app-root',
templateUrl: './appponent.html',
styleUrls: ['./appponent.css']
})
export class AppComponent {
title = 'app';
// Inject HttpClient into your ponent or service.
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('assets/api/list.json').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
I am trying to get the results
field from a json response however I get the error Property 'results' does not exist on type 'AppComponent
import { Component } from '@angular/core';
//in place where you wanted to use `HttpClient`
import { HttpClient } from '@angular/mon/http';
@Component({
selector: 'app-root',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.css']
})
export class AppComponent {
title = 'app';
// Inject HttpClient into your ponent or service.
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('assets/api/list.json').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
Share
Improve this question
asked Aug 15, 2017 at 16:59
mruss24mruss24
3672 gold badges7 silver badges14 bronze badges
2 Answers
Reset to default 6This is because Typescript piler checks that results
variable exists/initialized in a class, before using it in any method.
export class AppComponent {
title = 'app';
results: any[]; //define it here
// Inject HttpClient into your ponent or service.
constructor(private http: HttpClient) {}
ngOnInit(): void {
// Make the HTTP request:
this.http.get('assets/api/list.json').subscribe(data => {
// Read the result field from the JSON response.
this.results = data['results'];
});
}
}
Sometimes ['results'] will log the same error. (maybe for newer angular/ts versions)
this.http.get(url).subscribe((data:any) => {
// Read the result field from the JSON response.
//this.results = data.results;
this.results = data['results'];