Getting data from JSON file


#1

I have file of tips that I want to print. Every pressing on the button the next tip will print from the json file. So I wrote a function, but right to now the reading from the file doesn’t stop (attaching image of the log).
Any idea to get this information?
Many thanks!
The function .ts:

tips: any;

getDataJSON() {
      return new Promise(resolve => {
        this.http.get('/assets/advices.json') 
          .subscribe((data) => {
            resolve(data);
            console.log(data);
           this.getDataJSON().then(data => {
              this.tips = data;
          }, err => {
            console.log(err);
          });
      });
    })
  }

html:

<button ion-button block (click)="getDataJSON()">Fetch External Data</button>%E2%80%8F%E2%80%8F%D7%9C%D7%9B%D7%99%D7%93%D7%94


#2

Well, you didn’t listen to my advice in the last thread about this exact same problem, so you’ll probably ignore this as well, but here goes. That code is an absolute trainwreck. Throw it away completely and start over after reading this tour of heroes section. The principle and idioms used to retrieve heroes from an HTTP server will work for fetching JSON out of your assets folder.


#3

Thanks! I am not ignoring, just wanted to fix it for quick solution. I will read. Thanks for your help!


#4

Yes you are, because you are still abusing any and still failing to initialize tips at the point of declaration.