Firebase 承诺 - Query.once 失败:使用 1 个参数调用.预计至少 2

Firebase promise - Query.once failed: Was called with 1 argument. Expects at least 2

本文关键字:调用 参数 Query 承诺 once 失败 使用 Firebase      更新时间:2023-09-26

我正在尝试使用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 = {玩家名称:姓名,得分:50}

目前的数据库安排符合我的需求。

当您尝试在预承诺的 SDK 上使用已封装的 API 时,会出现此错误消息。

回传(其他答案建议的)适用于任何 2.x 版本的 Firebase SDK。

我们在 Firebase 的 JavaScript SDK 版本 2.4 中引入了一种使用承诺的替代语法。请参阅此 jsbin 中的once().then()示例:http://jsbin.com/qiranu/edit?js,console

您是否使用的是 Firebase JavaScript SDK 2.4 或更高版本(这是引入承诺的地方)?

不要使用 then .Once 将回调函数作为第二个参数:

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