Angular: Accessing data inside a JSON objectAngular HTML binding@Directive v/s @Component in Angularaccess key and value of object using *ngForGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themAcquireToken Observable errors before returning tokenAngular http requestPlaceholder in mat-autoComplete does not work as illustrated in the Angular Material Documentationng: ERROR: Can't resolve all parameters for component when trying to pass context into ComponentPortalGetting “Cannot read property 'http' of undefined” with Angular 7
How can bays and straits be determined in a procedurally generated map?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
Is it possible to do 50 km distance without any previous training?
Modeling an IP Address
Rock identification in KY
How can I make my BBEG immortal short of making them a Lich or Vampire?
What's the output of a record needle playing an out-of-speed record
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
Can a vampire attack twice with their claws using Multiattack?
Does detail obscure or enhance action?
Why can't I see bouncing of a switch on an oscilloscope?
Roll the carpet
How does one intimidate enemies without having the capacity for violence?
Is it legal for company to use my work email to pretend I still work there?
Arrow those variables!
Can you really stack all of this on an Opportunity Attack?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?
Is it possible to run Internet Explorer on OS X El Capitan?
Do I have a twin with permutated remainders?
Paid for article while in US on F-1 visa?
Which country benefited the most from UN Security Council vetoes?
How is it possible to have an ability score that is less than 3?
What is a clear way to write a bar that has an extra beat?
Angular: Accessing data inside a JSON object
Angular HTML binding@Directive v/s @Component in Angularaccess key and value of object using *ngForGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themAcquireToken Observable errors before returning tokenAngular http requestPlaceholder in mat-autoComplete does not work as illustrated in the Angular Material Documentationng: ERROR: Can't resolve all parameters for component when trying to pass context into ComponentPortalGetting “Cannot read property 'http' of undefined” with Angular 7
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a JSON object (pictured below) that I'm bring into an array called "eval".
In Angular, I am referencing this data via:
this.eval.list[0].main.temp
Main and temp are both fields in the array:
I am getting a compilation error, stating "Property 'list' does not exist on type 'Weather[]"
What am I doing wrong?
Weather's code is here:
export class Weather
cod:
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
constructor(i?: number, n?: string, h?: number, t?: number)
this.cod.city.id = i;
this.cod.city.name = n;
this.cod.list.main.humidity = h;
this.cod.list.main.temp = t;
Here is my component code:
import WeatherRestService from './../weatherRest.service';
import Component, OnInit from '@angular/core';
import Weather from '../models/weather';
@Component(
selector: 'app-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.css']
)
export class WidgetComponent implements OnInit
weather: Weather[];
eval: Weather[] = [];
light = 0;
constructor(public rest:WeatherRestService)
ngOnInit()
this.index();
index()
this.rest.index().subscribe(
weather =>
this.weather = weather;
this.eval = this.weather;
this.evaluate();
,
err =>
console.error('error retreiving properties');
console.error(err);
);
display()
console.log(this.weather);
this.evaluate();
evaluate()
if (this.eval.list[0].main.temp < 40)
this.light = 2;
else if (this.eval.list[0].main.temp >= 40 && this.eval.list[0].main.temp < 80)
this.light = 3;
else if (this.eval.list[0].main.temp >= 80)
this.light = 1;
console.log(this.eval);
console.log(this.eval.list[0].main.temp);
Here is the WeatherRest service (headers not being used, didn't seem to need them, got a 405 when I did:
import Injectable from '@angular/core';
import HttpClient, HttpHeaders from '@angular/common/http';
@Injectable(
providedIn: 'root'
)
export class WeatherRestService
private url = 'http://api.openweathermap.org/data/2.5/forecast?id=5417598&APPID=';
private auth = 'xxxxxxxxxxxxxxxxxx';
constructor(private http: HttpClient)
index()
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Basic ');
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Accept', 'application/json');
headers = headers.append('Content-Encoding', 'none');
headers = headers.append('Origin', 'http://localhost:4200');
return this.http.get<any[]>(this.url + this.auth + '&units=imperial');
// return this.http.get(this.url);
angular typescript
|
show 5 more comments
I have a JSON object (pictured below) that I'm bring into an array called "eval".
In Angular, I am referencing this data via:
this.eval.list[0].main.temp
Main and temp are both fields in the array:
I am getting a compilation error, stating "Property 'list' does not exist on type 'Weather[]"
What am I doing wrong?
Weather's code is here:
export class Weather
cod:
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
constructor(i?: number, n?: string, h?: number, t?: number)
this.cod.city.id = i;
this.cod.city.name = n;
this.cod.list.main.humidity = h;
this.cod.list.main.temp = t;
Here is my component code:
import WeatherRestService from './../weatherRest.service';
import Component, OnInit from '@angular/core';
import Weather from '../models/weather';
@Component(
selector: 'app-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.css']
)
export class WidgetComponent implements OnInit
weather: Weather[];
eval: Weather[] = [];
light = 0;
constructor(public rest:WeatherRestService)
ngOnInit()
this.index();
index()
this.rest.index().subscribe(
weather =>
this.weather = weather;
this.eval = this.weather;
this.evaluate();
,
err =>
console.error('error retreiving properties');
console.error(err);
);
display()
console.log(this.weather);
this.evaluate();
evaluate()
if (this.eval.list[0].main.temp < 40)
this.light = 2;
else if (this.eval.list[0].main.temp >= 40 && this.eval.list[0].main.temp < 80)
this.light = 3;
else if (this.eval.list[0].main.temp >= 80)
this.light = 1;
console.log(this.eval);
console.log(this.eval.list[0].main.temp);
Here is the WeatherRest service (headers not being used, didn't seem to need them, got a 405 when I did:
import Injectable from '@angular/core';
import HttpClient, HttpHeaders from '@angular/common/http';
@Injectable(
providedIn: 'root'
)
export class WeatherRestService
private url = 'http://api.openweathermap.org/data/2.5/forecast?id=5417598&APPID=';
private auth = 'xxxxxxxxxxxxxxxxxx';
constructor(private http: HttpClient)
index()
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Basic ');
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Accept', 'application/json');
headers = headers.append('Content-Encoding', 'none');
headers = headers.append('Origin', 'http://localhost:4200');
return this.http.get<any[]>(this.url + this.auth + '&units=imperial');
// return this.http.get(this.url);
angular typescript
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35
|
show 5 more comments
I have a JSON object (pictured below) that I'm bring into an array called "eval".
In Angular, I am referencing this data via:
this.eval.list[0].main.temp
Main and temp are both fields in the array:
I am getting a compilation error, stating "Property 'list' does not exist on type 'Weather[]"
What am I doing wrong?
Weather's code is here:
export class Weather
cod:
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
constructor(i?: number, n?: string, h?: number, t?: number)
this.cod.city.id = i;
this.cod.city.name = n;
this.cod.list.main.humidity = h;
this.cod.list.main.temp = t;
Here is my component code:
import WeatherRestService from './../weatherRest.service';
import Component, OnInit from '@angular/core';
import Weather from '../models/weather';
@Component(
selector: 'app-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.css']
)
export class WidgetComponent implements OnInit
weather: Weather[];
eval: Weather[] = [];
light = 0;
constructor(public rest:WeatherRestService)
ngOnInit()
this.index();
index()
this.rest.index().subscribe(
weather =>
this.weather = weather;
this.eval = this.weather;
this.evaluate();
,
err =>
console.error('error retreiving properties');
console.error(err);
);
display()
console.log(this.weather);
this.evaluate();
evaluate()
if (this.eval.list[0].main.temp < 40)
this.light = 2;
else if (this.eval.list[0].main.temp >= 40 && this.eval.list[0].main.temp < 80)
this.light = 3;
else if (this.eval.list[0].main.temp >= 80)
this.light = 1;
console.log(this.eval);
console.log(this.eval.list[0].main.temp);
Here is the WeatherRest service (headers not being used, didn't seem to need them, got a 405 when I did:
import Injectable from '@angular/core';
import HttpClient, HttpHeaders from '@angular/common/http';
@Injectable(
providedIn: 'root'
)
export class WeatherRestService
private url = 'http://api.openweathermap.org/data/2.5/forecast?id=5417598&APPID=';
private auth = 'xxxxxxxxxxxxxxxxxx';
constructor(private http: HttpClient)
index()
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Basic ');
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Accept', 'application/json');
headers = headers.append('Content-Encoding', 'none');
headers = headers.append('Origin', 'http://localhost:4200');
return this.http.get<any[]>(this.url + this.auth + '&units=imperial');
// return this.http.get(this.url);
angular typescript
I have a JSON object (pictured below) that I'm bring into an array called "eval".
In Angular, I am referencing this data via:
this.eval.list[0].main.temp
Main and temp are both fields in the array:
I am getting a compilation error, stating "Property 'list' does not exist on type 'Weather[]"
What am I doing wrong?
Weather's code is here:
export class Weather
cod:
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
constructor(i?: number, n?: string, h?: number, t?: number)
this.cod.city.id = i;
this.cod.city.name = n;
this.cod.list.main.humidity = h;
this.cod.list.main.temp = t;
Here is my component code:
import WeatherRestService from './../weatherRest.service';
import Component, OnInit from '@angular/core';
import Weather from '../models/weather';
@Component(
selector: 'app-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.css']
)
export class WidgetComponent implements OnInit
weather: Weather[];
eval: Weather[] = [];
light = 0;
constructor(public rest:WeatherRestService)
ngOnInit()
this.index();
index()
this.rest.index().subscribe(
weather =>
this.weather = weather;
this.eval = this.weather;
this.evaluate();
,
err =>
console.error('error retreiving properties');
console.error(err);
);
display()
console.log(this.weather);
this.evaluate();
evaluate()
if (this.eval.list[0].main.temp < 40)
this.light = 2;
else if (this.eval.list[0].main.temp >= 40 && this.eval.list[0].main.temp < 80)
this.light = 3;
else if (this.eval.list[0].main.temp >= 80)
this.light = 1;
console.log(this.eval);
console.log(this.eval.list[0].main.temp);
Here is the WeatherRest service (headers not being used, didn't seem to need them, got a 405 when I did:
import Injectable from '@angular/core';
import HttpClient, HttpHeaders from '@angular/common/http';
@Injectable(
providedIn: 'root'
)
export class WeatherRestService
private url = 'http://api.openweathermap.org/data/2.5/forecast?id=5417598&APPID=';
private auth = 'xxxxxxxxxxxxxxxxxx';
constructor(private http: HttpClient)
index()
let headers = new HttpHeaders();
headers = headers.append('Authorization', 'Basic ');
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Accept', 'application/json');
headers = headers.append('Content-Encoding', 'none');
headers = headers.append('Origin', 'http://localhost:4200');
return this.http.get<any[]>(this.url + this.auth + '&units=imperial');
// return this.http.get(this.url);
angular typescript
angular typescript
edited Mar 9 at 2:28
Rob Thompson
asked Mar 9 at 1:03
Rob ThompsonRob Thompson
256
256
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35
|
show 5 more comments
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35
|
show 5 more comments
1 Answer
1
active
oldest
votes
This ("Property 'list' does not exist on type 'Weather[]" ) error is coming because inside your Weather class you wrap everything inside a "cod" object. Also the Weather what you receive is not an array it is a simple object. Side note the constructor is not invoked at all. I would do the following changes:
Change the Weather class like this:
export class Weather
cod: string;
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
Then in component:
export class AppComponent implements OnInit {
weather: Weather;
eval: Weather;
After these changes you should get a working app. Also I would rename "eval" to something else as "eval" is a function in JS. See this link for details.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072986%2fangular-accessing-data-inside-a-json-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This ("Property 'list' does not exist on type 'Weather[]" ) error is coming because inside your Weather class you wrap everything inside a "cod" object. Also the Weather what you receive is not an array it is a simple object. Side note the constructor is not invoked at all. I would do the following changes:
Change the Weather class like this:
export class Weather
cod: string;
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
Then in component:
export class AppComponent implements OnInit {
weather: Weather;
eval: Weather;
After these changes you should get a working app. Also I would rename "eval" to something else as "eval" is a function in JS. See this link for details.
add a comment |
This ("Property 'list' does not exist on type 'Weather[]" ) error is coming because inside your Weather class you wrap everything inside a "cod" object. Also the Weather what you receive is not an array it is a simple object. Side note the constructor is not invoked at all. I would do the following changes:
Change the Weather class like this:
export class Weather
cod: string;
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
Then in component:
export class AppComponent implements OnInit {
weather: Weather;
eval: Weather;
After these changes you should get a working app. Also I would rename "eval" to something else as "eval" is a function in JS. See this link for details.
add a comment |
This ("Property 'list' does not exist on type 'Weather[]" ) error is coming because inside your Weather class you wrap everything inside a "cod" object. Also the Weather what you receive is not an array it is a simple object. Side note the constructor is not invoked at all. I would do the following changes:
Change the Weather class like this:
export class Weather
cod: string;
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
Then in component:
export class AppComponent implements OnInit {
weather: Weather;
eval: Weather;
After these changes you should get a working app. Also I would rename "eval" to something else as "eval" is a function in JS. See this link for details.
This ("Property 'list' does not exist on type 'Weather[]" ) error is coming because inside your Weather class you wrap everything inside a "cod" object. Also the Weather what you receive is not an array it is a simple object. Side note the constructor is not invoked at all. I would do the following changes:
Change the Weather class like this:
export class Weather
cod: string;
city:
id: number,
name: string
;
list:
main:
humidity: number,
temp: number,
;
Then in component:
export class AppComponent implements OnInit {
weather: Weather;
eval: Weather;
After these changes you should get a working app. Also I would rename "eval" to something else as "eval" is a function in JS. See this link for details.
answered Mar 9 at 11:28
robertrobert
6891120
6891120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072986%2fangular-accessing-data-inside-a-json-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
post your component code where you assign data from api to eval array
– Sudarshana Dayananda
Mar 9 at 1:15
something is not okay. In Weather class you have "cod" and "list" as a separate object. Inide constructor you access "list" as a nested object of "cod".
– robert
Mar 9 at 1:21
@SudarshanaDayananda, done
– Rob Thompson
Mar 9 at 1:26
@robert, I missed that, thank you. brb
– Rob Thompson
Mar 9 at 1:27
Are both city and list sub elements of cod ?
– Sudarshana Dayananda
Mar 9 at 1:35