I'm pretty new to js and this topic is tricky to me. How can I turn my code below to a function which takes one argument (username) retrieves data from database, and returns profile pic url? I mean 'newadmin' will be an argument passed to a function which i will call here, so i can use this profile pic in my entire website
const src = ref(database, `users/newadmin`);
const profpic = () => {
get(child(ref(database), "users/newadmin/profile_picture"))
.then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("no data");
}
})
.catch((error) => {
console.error(error);
});
};
profpic();
i tried this, returns undefined:
const src = ref(database, `users/newadmin`);
const profpic = () => {
var result;
get(child(ref(database), "users/newadmin/profile_picture"))
.then((snapshot) => {
if (snapshot.exists()) {
result = snapshot.val();
return result;
} else {
console.log("no data");
}
})
.catch((error) => {
console.error(error);
});
};
var x = profpic();
console.log(x);