If you want to transform the callback pattern into a promise-based pattern in order to retrieve a value from the callback, you can do so by implementing the following code:
async fetchImages(){
const reference = firebase.database().ref("/images");
const pictures = await new Promise((resolve, reject) => {
reference.orderByChild("name").equalTo("nature").once("child_added", function (snapshot) {
resolve(snapshot.val());
});
});
return pictures;
}
You can then fetch the images within your component as shown below:
...
async someMethod() {
this.pictures = await fetchImages();
}
Now, you are able to utilize the pictures
variable in your HTML.
Note: If your fetchImages
function already exists as a method within a component, you can assign the retrieved data directly to the pictures
property within the same function.
async fetchImages() {
const reference = firebase.database().ref("/images");
const snapshot = await reference.orderByChild("name").equalTo("nature").once("child_added");
// Snapshot is now accessible
this.pictures = snapshot.val();
}