I did not remember that I posted this question. I learned how to do this a long time ago.
Here is the code that will complete the task:
Services:
myFunction () {
var ref = firebase.database ().ref ('roots')
return new Promise ((resolve, reject) => {
ref.on ('value', function (snapshot) {
if (snapshot.val () == null) {
reject (null);
} else {
var list = new Array ();
snapshot.forEach (function (data) {
var item = {
key: data.key,
name1: data.val ().name1,
name2: data.val ().name2,
}
list.push (item);
});
resolve (list);
}
});
});
}
Component:
this.myService.myFunction ().then (objects => {
this.objects = objects;
for (let obj of this.objects) {
console.log (obj.key);
}
}).catch (error => {
alert ('Nothing found');
})
We wish you a happy coding!
source
share