0

I have so confused when handling the data from the server. I have a provider that fetch the data from the server through HTTP. I want to load the data before the page can be loaded. but the data will take a time to reflect on the page. Anyone please guide me to handling data from server

here is my code

workpage.ts

 loadWork(){
    this.work.loadwrk().then(hw=>{
      this.work = work ;
    })
  ionViewWillEnter(){
     this.loadWork();
    }

workprovider.ts

loadwrk():Promise<any>{
    return new Promise(resolve =>{
      this.storage.get('works').then(ow=>{
      this.http.get(---url----).subscribe((res)=>{ 
        let resData = res.json();
        if(JSON.stringify(ow) !=JSON.stringify(resData)){  
         console.log('i am new work');           
          resolve(resData);             
      }
      else{  
      resolve(ow); 
        console.log('i am old work');
      }

      })    
    })
  })
  }

2 Answers 2

1

Try like this :

loadwrk(): Promise<any> {
    return new Promise<any>((resolve, reject) => {
        this.storage.get('works').then(ow => {
            this.http.get(---url----).subscribe((res) => {
                let resonseData = res.json();
                if (JSON.stringify(ow) != JSON.stringify(resonseData)) {
                    console.log('i am new work');
                    resolve(resonseData);
                }
                else {
                    resolve(resonseData);
                    console.log('i am old work');
                }

            })
        })
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

converted response into json format like let resonseData = res.json();
sorry i have missed the code but still my issue not clear
1

you can use ionic loader or ngIf show value if fetch data is done

    <h1 *ngIf="item.Response !== null">#: {{item.Response.id}}</h1>

.

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
});
}

 ngAfterViewInit() {
  this.loadWork();}

and i see error in call function in this code

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
})


 ionViewWillEnter(){
     this.loadWork();
    }

is most be like this

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
});
}

 ionViewWillEnter(){
     this.loadWork();
    }

or you can make page show first and fetch the data and resend in NavParams this issues in ionic wen try view native or Map or ChartJs

Comments

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.