0

I got my json file and I am getting it on the service. Then I am trying to subscribe to it in the component, but in console.log(this.jsonObj) I get empty array. Also if I write console.log(data) - I get json data.

Service :

objUrl = 'assets/jsons/obs.json';
constructor(private http: HttpClient) {
  console.log('Hello ObjectsProvider Provider');
}
getObjectsJson() {
  return this.http.get(this.objUrl);
}

Component :

jsonObj = {};
this.object.getObjectsJson().subscribe((data =>
  this.jsonObj = data
))
console.log(this.jsonObj)
4
  • Change jsonObj = {} to jsonObj = [] if your return JSON is array of objects. Commented Oct 22, 2018 at 11:16
  • object is not assignable to type []any Commented Oct 22, 2018 at 11:17
  • Also notice that this.object.getObjectsJson().subscribe((data => this.jsonObj = data )) should be this.object.getObjectsJson().subscribe(data => { this.jsonObj = data }); Commented Oct 22, 2018 at 11:17
  • Use Observable Object instead of normal object Commented Oct 22, 2018 at 11:27

3 Answers 3

2

Issue

You are trying to get the Asynchronous data in Synchronous fashion. You are logging the data console.log(this.jsonObj) outside of Observable. So it will get executed without waiting for the result to come from API.

Fix

Just move the log or any code you want to execute the after API inside subscribe. So you your code will look like

jsonObj = [];
this.object.getObjectsJson().subscribe((data =>
  this.jsonObj = data;
  console.log(this.jsonObj);  //<-- data will be appear here.
))
Sign up to request clarification or add additional context in comments.

9 Comments

Yes but when I try to get it in html, it doesnt work, bcs html loads faster then jsonObj
It will work, whenever the value of jsonObj changes, Angular will makes change in UI even the html is already rendered before.
I get error in ngfor="let i of jsonObj" - cannot find differ[Object Object]. - in console I see 8 objects.
Initialize array as jsonObj = [];. Answer is corrected as well.
when I write jsonObj = [] , i get error on this.jsonObj = data , type object is not assignable to type any[].
|
0

The service method is asynchronous, so the code inside the subscribe() (which makes the assignment) executes at some time in the future, when the http call returns. Your log statement is outside of the subscription, so it happens before the assignment. Try putting the log statement inside the subscription, right after the assignment.

1 Comment

still empty :) thats not the problem I think
0

console.log(this.jsonObj) will run before the response of the server. You can work with it as it is. It will run perfectly. you can test it like this

<p *ngIf="jsonObj !== undefined">{{jsonObj.field}}</p>

if you want to check it with console.log, just add it in the subscription like this

this.http.getObjectsJson().subscribe((data => {
      this.jsonObj = data
      console.log(this.jsonObj)
    }));"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.