Reputation: 2343
I've encountered a problem while developing app in Ionic. I'm executing a query on the database using database provider and it is fine but it is probably called asynchronous because I get undefined.
After some checks and other logic I'm returning value from the database:
this.databaseProvider.getValueFor(saved.id)
.then(data => {
return data;
});
Is there any way to return data to Observable and subscribe to it? I'm quite new to Angular2 and don't know all concepts.
Upvotes: 0
Views: 3275
Reputation: 555
Observables are lazy so you need to subscribe it to get the data from it.
this.databaseProvider.getValueFor(saved.id)
.subscribe(data => {
console.log(data); //response
});
getValueFor(id): Observable<any> {
const url = 'http://yourAPI/'+id;
return this.http.get<any>(url)
.pipe(
map((res: any) => res.json()),
catchError(e => this.handleError(e))
);
}
Upvotes: 0
Reputation: 136134
You can use [**Observable.fromPromise**][1]
method to convert promise to Observable & then subscribe to Observable to retrieve data from it.
SomeMethod(){
return Observable.fromPromise(this.databaseProvider.getValueFor(saved.id))
}
//Usage
this.SomeMethod().subscribe(data => {
console.log("data", data)
})
Upvotes: 1