1

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);
kacperj513
  • 31
  • 5
  • What specifically has you stuck? Are you asking how to write a function at all? How to call a function? How to pass arguments to a function? How to return the response from an asynchronous operation in a function? Something else? Have you made an attempt, and can you indicate what specifically isn't working as expected in that attempt? – David Jun 19 '22 at 11:21
  • yes i made an attempt, been stuck on this for over 30 minutes, thats why i ask this question. as i said i am a newbie – kacperj513 Jun 19 '22 at 11:23
  • Can you update the question with that attempt and indicate what specifically didn't work as expected in that code? Put aside for a moment the overall goal of what you want to build and focus on specific pieces of functionality within the code. What function did you write, what parts worked as expected, and what specific part didn't? – David Jun 19 '22 at 11:27
  • sure, i still dont know why this returns undefined... – kacperj513 Jun 19 '22 at 12:09

0 Answers0