Mor*_*tch 0 javascript firebase firebase-realtime-database
Peep 总是从函数返回为 undefined。有人可以指出我的问题吗?在成功函数中,快照按预期返回。我认为这是一个范围界定问题。
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;
}
Run Code Online (Sandbox Code Playgroud)
在一次()方法是异步的,这就是为什么使用回调。您可以将回调作为参数传递给getPerson functionid 旁边。
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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2129 次 |
| 最近记录: |