How do I convert the function below to return a promise for proper handling in the Page where it is called?
getUploads() {
const rootDef = this.db.database.ref();
const uploadsRef = rootDef.child('userUploads').orderByChild('time');
const userRef = rootDef.child("userProfile");
var uploads = [];
uploadsRef.once("value").then((uploadSnaps) => {
uploadSnaps.forEach((uploadSnap) => {
var upload = uploadSnap.val();
userRef.child(uploadSnap.val().user).once("value").then((userSnap) => {
upload.displayName = userSnap.val().displayName;
upload.avatar = userSnap.val().avatar;
uploads.push(upload);
});
});
});
return uploads;
}
I attempted the code below, but encountered an error. How can I make the necessary modifications?
return new Promise((resolve, reject) => {
resolve(uploads);
});
To call this method, use the following syntax.
this.db.getUploads().then((uploads) => {
this.allUploads = uploads;
console.log(this.allUploads);
});