javascript - Firebase returns snapshot but can't access value - Stack Overflow

Peep is always returned from the function as undefined. Can someone point me to the problem? In the suc

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( "/" + 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;

}

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./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;

}
Share Improve this question edited Apr 1, 2016 at 17:39 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Apr 1, 2016 at 17:02 MoreScratchMoreScratch 3,0837 gold badges40 silver badges71 bronze badges 1
  • See stackoverflow./questions/14220321/… – Frank van Puffelen Commented Apr 1, 2016 at 17:33
Add a ment  | 

3 Answers 3

Reset to default 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./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);
});

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.

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./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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430192a4627356.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信