【问题标题】:Why does this Firebase ".indexOn" not work?为什么这个 Firebase“.indexOn”不起作用?
【发布时间】:2015-12-27 21:18:47
【问题描述】:

所以我有这样的数据:

pushNotifications {
    -K - RwEgd541PIGNOXXUH: {
        message: "Xiaoyu Hugh Hou bid at $900 on your task: New P..."
        notifyId: "facebook:10100683829966199"
        relatedTask: "-Jzl8XKaxGCv19nIOChm"
        timestamp: 1443594566599
    } - K - RwpcBlQa04VJT4Bwm: {
        message: "Sam Feldt bid at $1100 on your task: New Post g..."
        notifyId: "google:1043268349966197"
        relatedTask: "-Jzl8XKaxGCv19nIOChm"
        timestamp: 1443594721963
    } - K - RwuM3 - 0 G0q9I96WHg: {
        message: "Sam Feldt bid at $600 on your task: NO Firebase..."
        notifyId: "facebook:10100683829966199"
        relatedTask: "-JzTSk2TIlO46oVqJ1Nn"
        timestamp: 1443594741347
    }
}

在我的代码中,我需要获取下面某个“notifyId”的最新通知。代码如下:

ref.child('pushNotifications')
   .orderByChild("notifyId")
   .limitToLast(1)
   .on("child_added", function (snapshot) {
       console.log("Found a new notification...");
       sendPush(snapshot.val());
   });

在我的 firebase 安全规则中,我有这个索引规则:

 "pushNotifications": {
      ".read": "auth != null",
      ".write": "auth != null",
      "$pushId": {
        ".indexOn": ["notifyId", "timestamp"],
      }
    },

这里是 Firebase 文档所遵循的索引结构:docs

但是当我运行代码时,控制台仍然有日志

FIREBASE 警告:使用未指定的索引。考虑将 /pushNotifications 中的 ".indexOn": "notifyId" 添加到您的安全规则中以获得更好的性能

这没有任何意义,因为我已经这样做了。请帮忙!

【问题讨论】:

标签: firebase firebase-security firebase-realtime-database


【解决方案1】:

您需要将.indexOn 比您所做的高一级:

 "pushNotifications": {
    ".read": "auth != null",
    ".write": "auth != null",
    ".indexOn": ["notifyId", "timestamp"]
},

为了比较,请始终查看Firebase documentation on indexes,它使用了这个 JSON 示例:

"dinosaurs": {
  "lambeosaurus": {
    "height" : 2.1,
    "length" : 12.5,
    "weight": 5000
  },
  "stegosaurus": {
    "height" : 4,
    "length" : 9,
    "weight" : 2500
  }
}

使用此索引规则:

{
  "rules": {
    "dinosaurs": {
      ".indexOn": ["height", "length"]
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2013-03-10
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多