Database provider and it's promises

Hello,

I’m using this as reference: Ionic 2 - this keyword is undefined when promise is returned from data service

this.database.getTeams().then((response) => {

}, (err){

});

But if I try to use this it gives me the following error:

property then does not exist on void

This is because I have a method that has a promise in it, and it won’t return anything because it needs to wait for the promise. How is this fixable?

getTeams(){
 this.storage.get("teams").then((val) => {
  console.log(val);
  return val;    
  
}, (err) => {
  return err;
});

}

Thanks in advance.

Either do this

getTeams() {
    return this.storage.get("teams");
}

or

getTeams() {
	return new Promise((resolve, reject) => {
		this.storage.get("teams").then(
			val => {
				console.log(val);
				resolve(val);;
			}, 
			err => {
				reject(err);
			}
		);
	});
}

Because when you call this.database.getTeams().then, you’re expecting a promise from getTeams() and currently you’re only returning the value, buuut you have also placed your returns inside of the callback, meaning they would never make the getTeams() return, just the callback.

If you do either of the above, then your first bit of code should work just fine :slight_smile:

1 Like

That first option is much better. There’s no point in needlessly multiplying entities.