1

I'm currently using this function to get data from my Firebase project:

function getv(user)
{
    var ref = firebase.database().ref('/users/'+user+'/points');

    ref.once("value", function(snapshot) {
        console.log(snapshot.val());
    });
}

However, If I run this in the console, I get "undefined", and then the value:

enter image description here

If I try and assign it to a variable, and then get the value...

x = getv('Donut')
console.log(x)

I just get "undefined". I'm honestly at a complete loss as to why this is happening.

Any help is appreciated, I can provide more info if needed.

EDIT: Replacing console.log(snapshot.val()) with return snapshot.val() still returns undefined.

EDIT 2: I've tried turning my function into in async function, which turns it into a Promise, however it still returns undefined.

undefined promise

Async version:

async function getv(user)
{
    var ref = await firebase.database().ref('/users/'+user+'/points')

    ref.once("value", async function(snapshot) {
        return await snapshot.val()
    });
}
sethg
  • 159
  • 1
  • 1
  • 10
  • `getv` doesn't return anything, hence you get `undefined`. – maazadeeb Aug 28 '17 at 03:04
  • Replacing `console.log(snapshot.val())` with `return snapshot.val()` still returns undefined. – sethg Aug 28 '17 at 03:06
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – cartant Aug 28 '17 at 03:08
  • Unfortunately converting my function to use `async` did not work. I've updated my post with details. – sethg Aug 28 '17 at 05:13

0 Answers0