Angular: Can't access data inside a componentAngular HTML binding@Directive v/s @Component in AngularAngular and Typescript: Can't find namesAngular EXCEPTION: No provider for HttpAngular - Use pipes in services and componentsWhat is the equivalent of ngShow and ngHide in Angular?Angular - What is the meanings of module.id in component?Angular DI Error - EXCEPTION: Can't resolve all parametersHuge number of files generated for every Angular projectCan't bind to 'ngModel' since it isn't a known property of 'input'
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Infinite past with a beginning?
What is the offset in a seaplane's hull?
Can I interfere when another PC is about to be attacked?
Is it possible to make sharp wind that can cut stuff from afar?
The use of multiple foreign keys on same column in SQL Server
Copenhagen passport control - US citizen
What typically incentivizes a professor to change jobs to a lower ranking university?
Why don't electromagnetic waves interact with each other?
DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?
Why CLRS example on residual networks does not follows its formula?
"You are your self first supporter", a more proper way to say it
Possibly bubble sort algorithm
XeLaTeX and pdfLaTeX ignore hyphenation
Banach space and Hilbert space topology
What makes Graph invariants so useful/important?
How old can references or sources in a thesis be?
Modification to Chariots for Heavy Cavalry Analogue for 4-armed race
Pronouncing Dictionary.com's W.O.D "vade mecum" in English
Are tax years 2016 & 2017 back taxes deductible for tax year 2018?
Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)
How to add power-LED to my small amplifier?
Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).
What would the Romans have called "sorcery"?
Angular: Can't access data inside a component
Angular HTML binding@Directive v/s @Component in AngularAngular and Typescript: Can't find namesAngular EXCEPTION: No provider for HttpAngular - Use pipes in services and componentsWhat is the equivalent of ngShow and ngHide in Angular?Angular - What is the meanings of module.id in component?Angular DI Error - EXCEPTION: Can't resolve all parametersHuge number of files generated for every Angular projectCan't bind to 'ngModel' since it isn't a known property of 'input'
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am building a widget that pulls in a weather API, then presents an image based on the data in said API. I'm having trouble accessing the data in the component. I can't quite see where I am going wrong.
The flow here:
On init, I subscribe to the data. I then (try) to assign that to another array (eval). Then, I call the evaluate method and execute the business logic.
The problem: I know that I am incorrect in the way I'm accessing eval[0] but if I move it to list[0], it won't compile. The data feed includes a list array that has the temp field in it.
Thank you!
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();
this.evaluate();
index()
this.rest.index().subscribe(
weather => this.weather = weather; ,
err =>
console.error('error retreiving properties');
console.error(err);
);
this.eval = this.weather;
evaluate()
if (this.eval[0].list.main.temp < 20)
this.light = 1;
else if (this.eval[0].list.main.temp >= 20 && this.eval[0].list.main.temp < 50)
this.light = 2;
else if (this.eval[0].list.main.temp >= 50)
this.light = 3;
Here is an example of the JSON from the API:
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00","dt":1552035600,"main":"temp":267.23,"temp_min":265.757,"temp_max":267.23,"pressure":1015.09,"sea_level":1015.09,"grnd_level":750.66,"humidity":68,"temp_kf":1.47,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.32,"deg":226.502,"sys":"pod":"n","dt_txt":"2019-03-08 09:00:00","dt":1552424400,"main":"temp":275.391,"temp_min":275.391,"temp_max":275.391,"pressure":1004,"sea_level":1004,"grnd_level":744.59,"humidity":83,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13d"],"clouds":"all":88,"wind":"speed":1.58,"deg":82.5016,"rain":,"snow":"3h":0.425,"sys":"pod":"d","dt_txt":"2019-03-12 21:00:00"],"city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
Here, too, is the model I'm using:
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;
add a comment |
I am building a widget that pulls in a weather API, then presents an image based on the data in said API. I'm having trouble accessing the data in the component. I can't quite see where I am going wrong.
The flow here:
On init, I subscribe to the data. I then (try) to assign that to another array (eval). Then, I call the evaluate method and execute the business logic.
The problem: I know that I am incorrect in the way I'm accessing eval[0] but if I move it to list[0], it won't compile. The data feed includes a list array that has the temp field in it.
Thank you!
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();
this.evaluate();
index()
this.rest.index().subscribe(
weather => this.weather = weather; ,
err =>
console.error('error retreiving properties');
console.error(err);
);
this.eval = this.weather;
evaluate()
if (this.eval[0].list.main.temp < 20)
this.light = 1;
else if (this.eval[0].list.main.temp >= 20 && this.eval[0].list.main.temp < 50)
this.light = 2;
else if (this.eval[0].list.main.temp >= 50)
this.light = 3;
Here is an example of the JSON from the API:
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00","dt":1552035600,"main":"temp":267.23,"temp_min":265.757,"temp_max":267.23,"pressure":1015.09,"sea_level":1015.09,"grnd_level":750.66,"humidity":68,"temp_kf":1.47,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.32,"deg":226.502,"sys":"pod":"n","dt_txt":"2019-03-08 09:00:00","dt":1552424400,"main":"temp":275.391,"temp_min":275.391,"temp_max":275.391,"pressure":1004,"sea_level":1004,"grnd_level":744.59,"humidity":83,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13d"],"clouds":"all":88,"wind":"speed":1.58,"deg":82.5016,"rain":,"snow":"3h":0.425,"sys":"pod":"d","dt_txt":"2019-03-12 21:00:00"],"city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
Here, too, is the model I'm using:
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;
1
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
1
That may not be helpful. If there are extra fields in object, please remove it and post avalidJSON in the question.
– Sachin Gupta
Mar 8 at 6:15
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25
add a comment |
I am building a widget that pulls in a weather API, then presents an image based on the data in said API. I'm having trouble accessing the data in the component. I can't quite see where I am going wrong.
The flow here:
On init, I subscribe to the data. I then (try) to assign that to another array (eval). Then, I call the evaluate method and execute the business logic.
The problem: I know that I am incorrect in the way I'm accessing eval[0] but if I move it to list[0], it won't compile. The data feed includes a list array that has the temp field in it.
Thank you!
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();
this.evaluate();
index()
this.rest.index().subscribe(
weather => this.weather = weather; ,
err =>
console.error('error retreiving properties');
console.error(err);
);
this.eval = this.weather;
evaluate()
if (this.eval[0].list.main.temp < 20)
this.light = 1;
else if (this.eval[0].list.main.temp >= 20 && this.eval[0].list.main.temp < 50)
this.light = 2;
else if (this.eval[0].list.main.temp >= 50)
this.light = 3;
Here is an example of the JSON from the API:
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00","dt":1552035600,"main":"temp":267.23,"temp_min":265.757,"temp_max":267.23,"pressure":1015.09,"sea_level":1015.09,"grnd_level":750.66,"humidity":68,"temp_kf":1.47,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.32,"deg":226.502,"sys":"pod":"n","dt_txt":"2019-03-08 09:00:00","dt":1552424400,"main":"temp":275.391,"temp_min":275.391,"temp_max":275.391,"pressure":1004,"sea_level":1004,"grnd_level":744.59,"humidity":83,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13d"],"clouds":"all":88,"wind":"speed":1.58,"deg":82.5016,"rain":,"snow":"3h":0.425,"sys":"pod":"d","dt_txt":"2019-03-12 21:00:00"],"city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
Here, too, is the model I'm using:
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;
I am building a widget that pulls in a weather API, then presents an image based on the data in said API. I'm having trouble accessing the data in the component. I can't quite see where I am going wrong.
The flow here:
On init, I subscribe to the data. I then (try) to assign that to another array (eval). Then, I call the evaluate method and execute the business logic.
The problem: I know that I am incorrect in the way I'm accessing eval[0] but if I move it to list[0], it won't compile. The data feed includes a list array that has the temp field in it.
Thank you!
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();
this.evaluate();
index()
this.rest.index().subscribe(
weather => this.weather = weather; ,
err =>
console.error('error retreiving properties');
console.error(err);
);
this.eval = this.weather;
evaluate()
if (this.eval[0].list.main.temp < 20)
this.light = 1;
else if (this.eval[0].list.main.temp >= 20 && this.eval[0].list.main.temp < 50)
this.light = 2;
else if (this.eval[0].list.main.temp >= 50)
this.light = 3;
Here is an example of the JSON from the API:
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00","dt":1552035600,"main":"temp":267.23,"temp_min":265.757,"temp_max":267.23,"pressure":1015.09,"sea_level":1015.09,"grnd_level":750.66,"humidity":68,"temp_kf":1.47,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.32,"deg":226.502,"sys":"pod":"n","dt_txt":"2019-03-08 09:00:00","dt":1552424400,"main":"temp":275.391,"temp_min":275.391,"temp_max":275.391,"pressure":1004,"sea_level":1004,"grnd_level":744.59,"humidity":83,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13d"],"clouds":"all":88,"wind":"speed":1.58,"deg":82.5016,"rain":,"snow":"3h":0.425,"sys":"pod":"d","dt_txt":"2019-03-12 21:00:00"],"city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
Here, too, is the model I'm using:
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;
edited Mar 8 at 6:29
Rob Thompson
asked Mar 8 at 5:57
Rob ThompsonRob Thompson
256
256
1
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
1
That may not be helpful. If there are extra fields in object, please remove it and post avalidJSON in the question.
– Sachin Gupta
Mar 8 at 6:15
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25
add a comment |
1
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
1
That may not be helpful. If there are extra fields in object, please remove it and post avalidJSON in the question.
– Sachin Gupta
Mar 8 at 6:15
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25
1
1
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
1
1
That may not be helpful. If there are extra fields in object, please remove it and post a
valid JSON in the question.– Sachin Gupta
Mar 8 at 6:15
That may not be helpful. If there are extra fields in object, please remove it and post a
valid JSON in the question.– Sachin Gupta
Mar 8 at 6:15
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25
add a comment |
2 Answers
2
active
oldest
votes
The code has several problems related asynchronious data fetching:
this.eval = this.weather;is dependent on subscription, so you need to assign it inside ofsubscribe- the method
evaluateis dependant onthis.eval
You need to put all operations that depend on this.rest.index inside of subscribe to it:
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);
);
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot accessthis.eval[0].list.main.tempis thatthis.evalis an object, not an array. Try it this way:this.eval.list[0].main.temp
– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
@RobThompson I guess you need to add inside yourtsconfig.jsonsettings"types": ["jest"], like so:"compilerOptions": "types": ["jest"]
– Vadi
Mar 8 at 7:02
|
show 5 more comments
You could check the sample blew. we could access this.eval in the complete callback function. We must use Non-blocking style for this kind of stuff, the code will not be blocked and wait. We could not access this.eval like we do in sychronize style.
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err),
**() => console.log('Observer got a complete notification')**
);
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%2f55057497%2fangular-cant-access-data-inside-a-component%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The code has several problems related asynchronious data fetching:
this.eval = this.weather;is dependent on subscription, so you need to assign it inside ofsubscribe- the method
evaluateis dependant onthis.eval
You need to put all operations that depend on this.rest.index inside of subscribe to it:
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);
);
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot accessthis.eval[0].list.main.tempis thatthis.evalis an object, not an array. Try it this way:this.eval.list[0].main.temp
– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
@RobThompson I guess you need to add inside yourtsconfig.jsonsettings"types": ["jest"], like so:"compilerOptions": "types": ["jest"]
– Vadi
Mar 8 at 7:02
|
show 5 more comments
The code has several problems related asynchronious data fetching:
this.eval = this.weather;is dependent on subscription, so you need to assign it inside ofsubscribe- the method
evaluateis dependant onthis.eval
You need to put all operations that depend on this.rest.index inside of subscribe to it:
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);
);
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot accessthis.eval[0].list.main.tempis thatthis.evalis an object, not an array. Try it this way:this.eval.list[0].main.temp
– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
@RobThompson I guess you need to add inside yourtsconfig.jsonsettings"types": ["jest"], like so:"compilerOptions": "types": ["jest"]
– Vadi
Mar 8 at 7:02
|
show 5 more comments
The code has several problems related asynchronious data fetching:
this.eval = this.weather;is dependent on subscription, so you need to assign it inside ofsubscribe- the method
evaluateis dependant onthis.eval
You need to put all operations that depend on this.rest.index inside of subscribe to it:
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);
);
The code has several problems related asynchronious data fetching:
this.eval = this.weather;is dependent on subscription, so you need to assign it inside ofsubscribe- the method
evaluateis dependant onthis.eval
You need to put all operations that depend on this.rest.index inside of subscribe to it:
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);
);
edited Mar 8 at 6:16
answered Mar 8 at 6:03
VadiVadi
1,154516
1,154516
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot accessthis.eval[0].list.main.tempis thatthis.evalis an object, not an array. Try it this way:this.eval.list[0].main.temp
– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
@RobThompson I guess you need to add inside yourtsconfig.jsonsettings"types": ["jest"], like so:"compilerOptions": "types": ["jest"]
– Vadi
Mar 8 at 7:02
|
show 5 more comments
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot accessthis.eval[0].list.main.tempis thatthis.evalis an object, not an array. Try it this way:this.eval.list[0].main.temp
– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
@RobThompson I guess you need to add inside yourtsconfig.jsonsettings"types": ["jest"], like so:"compilerOptions": "types": ["jest"]
– Vadi
Mar 8 at 7:02
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
Thank you. This is populating the data into eval now. I suspected it was async related but didn't know how to overcome that. May I ask, how do I then access the data in this.eval? I think I am missing something in the JSON access (e.g., this.eval[0].list.main.temp doesn't work). I can access in the HTML just not in the .ts
– Rob Thompson
Mar 8 at 6:21
@RobThompson the problem you cannot access
this.eval[0].list.main.temp is that this.eval is an object, not an array. Try it this way: this.eval.list[0].main.temp– Vadi
Mar 8 at 6:40
@RobThompson the problem you cannot access
this.eval[0].list.main.temp is that this.eval is an object, not an array. Try it this way: this.eval.list[0].main.temp– Vadi
Mar 8 at 6:40
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
That worked. Thank you.
– Rob Thompson
Mar 8 at 6:42
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
@RobThompson feel free to accept/upvote the answer if it helped)
– Vadi
Mar 8 at 6:45
1
1
@RobThompson I guess you need to add inside your
tsconfig.json settings "types": ["jest"], like so: "compilerOptions": "types": ["jest"] – Vadi
Mar 8 at 7:02
@RobThompson I guess you need to add inside your
tsconfig.json settings "types": ["jest"], like so: "compilerOptions": "types": ["jest"] – Vadi
Mar 8 at 7:02
|
show 5 more comments
You could check the sample blew. we could access this.eval in the complete callback function. We must use Non-blocking style for this kind of stuff, the code will not be blocked and wait. We could not access this.eval like we do in sychronize style.
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err),
**() => console.log('Observer got a complete notification')**
);
add a comment |
You could check the sample blew. we could access this.eval in the complete callback function. We must use Non-blocking style for this kind of stuff, the code will not be blocked and wait. We could not access this.eval like we do in sychronize style.
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err),
**() => console.log('Observer got a complete notification')**
);
add a comment |
You could check the sample blew. we could access this.eval in the complete callback function. We must use Non-blocking style for this kind of stuff, the code will not be blocked and wait. We could not access this.eval like we do in sychronize style.
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err),
**() => console.log('Observer got a complete notification')**
);
You could check the sample blew. we could access this.eval in the complete callback function. We must use Non-blocking style for this kind of stuff, the code will not be blocked and wait. We could not access this.eval like we do in sychronize style.
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err),
**() => console.log('Observer got a complete notification')**
);
answered Mar 8 at 6:33
todaynoworktodaynowork
184
184
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%2f55057497%2fangular-cant-access-data-inside-a-component%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
1
Can you provide some sample data of the structure that the service gives you.
– Sachin Gupta
Mar 8 at 6:00
"cod":"200","message":0.008,"cnt":38,"list":["dt":1552024800,"main":"temp":268.93,"temp_min":266.967,"temp_max":268.93,"pressure":1015.34,"sea_level":1015.34,"grnd_level":751.71,"humidity":72,"temp_kf":1.96,"weather":["id":800,"main":"Clear","description":"clear sky","icon":"01n"],"clouds":"all":0,"wind":"speed":2.26,"deg":242.501,"sys":"pod":"n","dt_txt":"2019-03-08 06:00:00", .... "city":"id":5417598,"name":"Colorado Springs","coord":"lat":38.8339,"lon":-104.8214,"country":"US"
– Rob Thompson
Mar 8 at 6:12
The elipses on the second to last light is just repeating segments of the same structure just forward forecasts.
– Rob Thompson
Mar 8 at 6:13
1
That may not be helpful. If there are extra fields in object, please remove it and post a
validJSON in the question.– Sachin Gupta
Mar 8 at 6:15
I will try right now, thank you.
– Rob Thompson
Mar 8 at 6:25