0

Peep is always returned from the function as undefined. Can someone point me to the problem? In the success function the snapshot is returned as expected. I think it's a scoping issue.

function getPerson( id ) {

    var ref = new Firebase( "https://foo.firebaseio.com/people/" + id ),
    peep;

    // Attach an asynchronous callback to read the data at our people reference
    ref.once( "value", function( snapshot ) {
        //success
        peep = snapshot;

    }, function ( errorObject ) {
        //error
        //console.log( "The read failed: " + errorObject.code );
    });

    return peep;

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MoreScratch
  • 2,933
  • 6
  • 34
  • 65

3 Answers3

5

The once() method is asynchronous that is why callback is used. You can pass a callback as a parameter to the getPerson function alongside the id.

function getPerson(id, callback) {
  var ref = new Firebase( "https://foo.firebaseio.com/people/" + id );

  ref.once( "value", function(snapshot) {
    var peep = snapshot;
      // error will be null, and peep will contain the snapshot
      callback(null, peep);
    }, function (error) {
      // error wil be an Object
      callback(error)
  });
}

getperson('9234342', function (err, result) {
  console.log(result);
});
Akinjide
  • 2,723
  • 22
  • 28
2

Yes it should be because the success callback is not fired immediately. It receives data then get into it.

Try returning the peep inside the callback like.

return ref.once( "value", function( snapshot ) {
        //success
        peep = snapshot;
        return peep;

    }, function ( errorObject ) {
        //error
        //console.log( "The read failed: " + errorObject.code );
    });

Hope it helps.

M. Junaid Salaat
  • 3,765
  • 1
  • 23
  • 25
1

The once() method is asynchronous which is why a callback is used. You are returning peep before it even has a value. You need to return peep after it is defined in the callback.

function getPerson( id ) {

    var ref = new Firebase( "https://foo.firebaseio.com/people/" + id ),
    peep;

    // Attach an asynchronous callback to read the data at our people reference
    return ref.once( "value", function( snapshot ) {
        //success
        peep = snapshot;
        return peep;
    }, function ( errorObject ) {
        //error
        //console.log( "The read failed: " + errorObject.code );
    });
}

And then use it like this:

getPerson('9234342').then(function(snapshot) {
  console.log(snapshot.val());
}).catch(function(error) {
  console.error(error);
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Maverick976
  • 528
  • 1
  • 4
  • 11
  • That `return peep` will still not work. See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Frank van Puffelen Apr 01 '16 at 17:31
  • Sorry, but this still won't work. The `return peep` returns a value, but nobody is waiting for that value. If you want, you can return the promise. Simply `return ref.once("value")`. – Frank van Puffelen Apr 01 '16 at 17:38
  • I added a usage section to your answer, see [this working jsbin](http://jsbin.com/qebovo/edit?js,console). Note that this requires Firebase JavaScript SDK 2.4 or higher, since it relies on promises. – Frank van Puffelen Apr 01 '16 at 18:00