I'm trying to clean up my Ionic app and move redundant functions into a provider. Below is an example of what most of my API functions look like (except I've stripped out a lot of irrelevant stuff for brevity) and they are working perfectly.
getDataFromApi() {
....
Promise.all([
...
]).then((result) => {
let headers = new Headers();
...
let body = new FormData();
...
this.http.post(url, body, headers)
.map(res => res.json())
.subscribe(data => {
if (data == '{}') {
this.mydata = [];
} else {
this.mydata = data;
}
}, error => { });
});
}
So what I've done is move the function into the provider and changed it like so
getDataFromApi() {
....
Promise.all([
...
]).then((result) => {
let headers = new Headers();
...
let body = new FormData();
...
return this.http.post(url, body, headers));
});
}
And then inside the page's constructor I'm calling it like this
this.service.getDataFromApi()
.map(res => res.json())
.subscribe(data => {
if (data == '{}') {
this.mydata = [];
} else {
this.mydata = data;
}
}, error => { });
Obviously that's not working. I've been pouring over SO posts for 2 days now and can't get other people's examples and answers to work properly either. I've tried mapping in the provider and subscribing when calling it in the page, but no matter what I try just keep receiving errors. I know this function is returning data because I can see it before moving it into the provider.
What am I doing wrong?