我正在尝试使用promises从Firebase填充一些数据。这是数据库结构:
- domain name(or something)
|--highscore
|--Foo: 50
|--Bar: 60
代码:
var arr=[];
highscoreRef.child('highscore').once('value').then(function(snapshot) {
snapshot.forEach(function(data) {
arr.push({playerName: data.key(), score: data.val()});
});
}, function(error) {
console.error(error);
});
我得到Uncaught Error: Query.once failed: Was called with 1 argument. Expects at least 2.
这是否意味着我必须向Foo和Bar添加至少2个属性?防爆。 Foo = {playerName:name,score:50}
当前的数据库安排符合我的需求。
答案 0 :(得分:8)
当您尝试在预先授权的SDK上使用Promisified API时会出现此错误消息。
回调(其他答案已建议)将适用于任何2.x版本的Firebase SDK。
我们在Firebase的JavaScript SDK 2.4版中引入了使用promises的替代语法。请参阅此jsbin中的once().then()
示例:http://jsbin.com/qiranu/edit?js,console
您使用的是Firebase JavaScript SDK 2.4或更高版本(那是推出承诺的地方)吗?
答案 1 :(得分:0)
请勿使用then
。一旦将回调函数作为第二个参数:
https://www.firebase.com/docs/web/api/query/once.html
var arr=[];
highscoreRef.child('highscore').once('value', function(snapshot) {
snapshot.forEach(function(data) {
arr.push({playerName: data.key(), score: data.val()});
});
}, function(error) {
console.error(error);
});