Since the http.get method is async, if you want that line to be executed when the data is ready, place it inside of the then. You can also simplify your buscar(...) method like this:
buscar(ruta): Promise<any> {
return this.http.get(ruta)
.map(res => res.json()) // Get the body of the response
.toPromise() // Convert the observable into a promise
.then(
response => { // Success callback
this.results = response;
console.log(response);
// Since the http get is async, you need to place the logic
// to save the data in the storage here!
// ...
// this.storage.set('data', JSON.stringify(response));
// ...
console.log("Last line");
},
error => { // Error callback
// TODO: Handle error
// ...
console.log(error);
return error;
});
}
EDIT
I'm calling this method from one
page:this.servicioApi.buscar(this.path); After that I want to access
to the data that I saved in storage, but it returns null (And the get
to access to the storage variable executes before the call to the
method "buscar"), and if I access to the data from another page it
returns the JSON
Let me modify again the method, in order to return the response:
buscar(ruta): Promise<any> {
return this.http.get(ruta)
.map(res => res.json()) // Get the body of the response
.toPromise() // Convert the observable into a promise
.then(
response => { // Success callback
this.results = response;
console.log(response);
// Since the http get is async, you need to place the logic
// to save the data in the storage here!
// ...
// this.storage.set('data', JSON.stringify(response));
// ...
console.log("Last line");
// UPDATE: return the response
return response;
},
error => { // Error callback
// TODO: Handle error
// ...
console.log(error);
// UPDATE: return null if there was an error
return null;
});
}
So now we're returning the response (or null if there was an error). That gives us two ways of getting that data when calling that method:
1) Getting the response from the callback
this.servicioApi.buscar(this.path).then(res => {
if(res) {
// Here you can use the response
// ...
} else {
// Here you can do something if there was an error
// ...
}
});
2) Getting the response from the storage
this.servicioApi.buscar(this.path).then(() => {
this.storage.get('data').then(responseJson => {
if(responseJson) {
// Parse the response since it is a JSON
let response = JSON.parse(responseJson);
// Here you can use the response
// ...
}
});
});
EDIT II
Just like @Sampath mentioned, you could just use observables (which is Angular recommended way), like this:
buscar(ruta): Observable<any> {
return this.http.get(ruta)
.map(res => res.json()) // Get the body of the response
.map(response => {
this.results = response;
console.log(response);
// Since the http get is async, you need to place the logic
// to save the data in the storage here!
// ...
// this.storage.set('data', JSON.stringify(response));
// ...
return response;
});
}
And then just subscribe to that method:
this.servicioApi.buscar(this.path)
.subscribe(
res => {
// Here you can use the response
// ...
},
error => {
// TODO: Handle error
// ...
console.log(error);
});