0

So I have this Angular app, and right now we're just loading data from a json file until my friends get the database up and running, but I"m having an issue: the data never seems to load. Can anybody look at my code and tell me what I'm doing wrong? Here's my data service code:

export class DataService {
  public positionsData: Position = {
      id: 'empty',
      name: 'empty',
      title: 'empty',
      routing: 'empty',
      email: 'empty',
      children: []
  };
  public loaded = false;

  constructor(private http: HttpClient) {
    setTimeout(() => {this.loadData}, 15);
  }

  loadData(): void {
    this.http.get<Position>('./sponsorview_data_4_22.json').subscribe( (updatedPositions: Position) => { this.positionsData = updatedPositions; this.loaded = true;});
  }

and here's the component where I need that data:

ngOnInit(): void {
    this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account));
    if(!this.dataService.loaded) {
        this.datasource = this.dataService.getPositions();
        setTimeout(() => {this.loadDatasource();}, 370)
    }

}

  loadDatasource(): void {
    console.log('one round in loadDatasource');
    if(this.dataService.loaded) {
        console.log("data loaded and initialized");
        this.datasource = this.dataService.getPositionsToDepth(this.dataService.getPositions(), 3);
        this.rootViewNodeId = this.datasource.id;
        this.focusedNodeId = this.datasource.id;setTimeout(this.loadDatasource, 37);

    } else {
        setTimeout(() => {this.loadDatasource()}, 370)
        console.log("data not yet loaded");
    }

  }
1
  • Could you also post sponsorview_data_4_22.json data for making working example solution?? Also what is the issue you are facing here?? Commented Apr 24, 2020 at 1:57

1 Answer 1

2

I don't think you should be calling an Http request on a JSON without a server. Alternatively, you can get the JSON data directly like this.

this.positionsData = JSON.parse('./sponsorview_data_4_22.json');
Sign up to request clarification or add additional context in comments.

5 Comments

this still doesn't actually completely fix my problem. . . In the component where I use the data, it seems like the data never loads. I'll update my question to show you
furthermore, when I call JSON.parse('./sponsorview_data_4_22.json'); it tries to parse the string "sponsorview_data_4_22.json" as json
should be like what?
Since there are no more subscriptions your loadData() method in DataService should be look like this loadData(): void { let updatedPositions = JSON.parse('./sponsorview_data_4_22.json'); if(updatedPositions) { this.positionsData = updatedPositions; this.loaded = true } return this.positionsData || null }
but like I said above, JSON.parse isn't working for me. It's trying to interpret the file path as JSON instead of loading the file there and interpreting that as JSON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.