Angular: Accessing data inside a JSON object Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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

How to make triangles with rounded sides and corners? (squircle with 3 sides)

Does the universe have a fixed centre of mass?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

The test team as an enemy of development? And how can this be avoided?

What is a more techy Technical Writer job title that isn't cutesy or confusing?

What is the proper term for etching or digging of wall to hide conduit of cables

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

Noise in Eigenvalues plot

Why does BitLocker not use RSA?

NIntegrate on a solution of a matrix ODE

One-one communication

Shimano 105 brifters (5800) and Avid BB5 compatibility

What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?

Why do the Z-fighters hide their power?

How do I say "this must not happen"?

By what mechanism was the 2017 UK General Election called?

Is there a verb for listening stealthily?

Any stored/leased 737s that could substitute for grounded MAXs?

Why can't fire hurt Daenerys but it did to Jon Snow in season 1?

The Nth Gryphon Number

How do I find my Spellcasting Ability for my D&D character?

First paper to introduce the "principal-agent problem"

Why is there so little support for joining EFTA in the British parliament?

.bashrc alias for a command with fixed second parameter



Angular: Accessing data inside a JSON object



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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;








1















I have a JSON object (pictured below) that I'm bring into an array called "eval".



enter image description here



In Angular, I am referencing this data via:



this.eval.list[0].main.temp


Main and temp are both fields in the array:



enter image description here



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);












share|improve this question
























  • 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

















1















I have a JSON object (pictured below) that I'm bring into an array called "eval".



enter image description here



In Angular, I am referencing this data via:



this.eval.list[0].main.temp


Main and temp are both fields in the array:



enter image description here



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);












share|improve this question
























  • 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













1












1








1








I have a JSON object (pictured below) that I'm bring into an array called "eval".



enter image description here



In Angular, I am referencing this data via:



this.eval.list[0].main.temp


Main and temp are both fields in the array:



enter image description here



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);












share|improve this question
















I have a JSON object (pictured below) that I'm bring into an array called "eval".



enter image description here



In Angular, I am referencing this data via:



this.eval.list[0].main.temp


Main and temp are both fields in the array:



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer



























      1














      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.






      share|improve this answer

























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 11:28









        robertrobert

        7231120




        7231120





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко