【问题标题】:Firebase no index defined even when using .indexOn correctly即使正确使用 .indexOn,Firebase 也没有定义索引
【发布时间】:2021-08-19 01:52:55
【问题描述】:

我已尝试使用 Firebase (2.2.5) API / .orderByChild('score') 操作,但我不断收到

No index defined for score

虽然 Firebase 的文档指出我在使用 WebSocket API 时不需要为分数创建索引,但我确实创建了它。标记:

规则

{
"rules": {
    ".read": false,
    ".write": false,

    /* index players by score */
    "Players": {
      ".read": true,
      ".indexOn": "score",
      "$playerID": {
        ".write": "!data.exists() || data.child('uid').val() === auth.uid"
      }
    }
  }
}

数据

{
    "Players": {

      "-JpqOuoaHmcA2EngEvSZ" : {
        ".priority" : 0.0,
        "avatar" : "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRS9Yh2lWzGP1oojIwNVV2JQUZYFX4_4pxvaADz8TfDF_WGrHbs",
        "draw" : 9,
        "lastTimeAccessed" : 1432705280027,
        "lost" : 7,
        "name" : "Hans",
        "score" : 126,
        "won" : 36
      },

      "-JpzVTpXZUZG-V0qJCDT" : {
        ".priority" : 2.0,
        "avatar" : "http://static.wixstatic.com/media/6a9a03_f979928a006c4d2ebc179c1c627dc119.png_srz_300_300_75_22_0.50_1.20_0.00_png_srz",
        "draw" : 0,
        "lastTimeAccessed" : 1432705280532,
        "lost" : 25,
        "name" : "Safari",
        "score" : 9,
        "won" : 3
      }
    }
}

通话

我有一个指向 http://theapp.firebaseio.com/Players 的 Firebase() 对象,为此我调用 orderByChild('score')。我希望这能奏效。

期待一些新鲜的观点或提示。

【问题讨论】:

  • 进一步调试表明,当我在使用dataSnapshot.forEach() 方法解析dataSnapshot。
  • 实际的解决方法是跳过 dataSnapshot 上的 forEach 方法,只处理 dataSnapshot.val()。我确实想知道为什么dataSnapshot.forEach() 会崩溃。因为这个方法在不使用orderByChild()时工作得很好。
  • 您能否编辑问题以包含给出错误的代码和没有的代码?
  • 这是 Firebase 库中的一个错误。您能否提供一个重现此代码的示例,我们在重现此问题时遇到问题,并且可能是某种调用组合触发了此...

标签: firebase


【解决方案1】:

我在使用这段代码时遇到了这个错误:

const snap = await admin
    .database()
    .ref("keepAlive")
    .orderByChild("lastSeen")
    .get();

  snap.forEach((result) => {
    console.log({ result });
  });

把代码改成这样就可以了:

const snap = await admin
    .database()
    .ref("keepAlive")
    .orderByChild("lastSeen")
    .once("value");

  snap.forEach((result) => {
    console.log({ result: result.val() });
  });

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2019-12-13
    • 2018-06-13
    • 2012-10-10
    • 2021-09-17
    • 2013-06-29
    • 2020-03-25
    • 2021-02-22
    • 2016-04-30
    相关资源
    最近更新 更多