Reputation: 47
I'm storing data in a Firebase realtime database and I'm trying to get the stored data and it seems I'm not getting it correctly - I need any assistance.
auth.service.ts
:
get_Data() {
return firebase.database().ref('transfer/').on('value', (snapshot) => {
this.transfer = snapshot.val();
console.log('snapshot here', this.transfer);
})
}
returns
app.component.ts
:
dataList(){
this.list = this.authService.get_Data();
console.log('got you', this.list)
}
returning undefined
...
Upvotes: 1
Views: 1142
Reputation: 80952
Try the following:
get_Data() {
return new Promise((resolve, reject) => {
firebase.database().ref('transfer/').on('value', (snapshot) => {
this.transfer = snapshot.val();
resolve(snapshot.val());
console.log('snapshot here', this.transfer);
});
});
}
Then inside your component, you can do:
dataList(){
this.authService.get_Data().then((value) => {
console.log(value);
});
}
Check here for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
Upvotes: 1