【问题标题】:How to write .indexOn for dynamic keys in firebase?如何为firebase中的动态键编写.indexOn?
【发布时间】:2016-01-17 08:36:01
【问题描述】:

我有这样的数据设置:

Tasks:{
  Group 1:{
    pushid1:{
      date: 18921949021,
      title: "Do something"
    },
    pushid2:{
      date: 18921949021,
      title: "Do something else"
    }
  },
  Group 2:{
    pushid3:{
      date: 18921949021,
      title: "Do another thing"
    }
  }
}

我按日期引用它,所以我只会在特定时间之后获得任务。

var now = Date.now();
var taskRef = new Firebase(FB + "/tasks/" + thisGroup);
taskRef.orderByChild('date').startAt(now).on("child_added", function(snap){
  console.log(snap.val());
});

这一切都很好,对于每个组,我都会收到一条警告消息,内容如下:

FIREBASE WARNING: Using an unspecified index.  Consider adding ".indexOn": "date" at tasks/Group 1 to your security rules for better performance

如何将其添加到我的安全规则中?我无法为用户创建的每个组添加一个,是否存在某种类型的通配符条目?

【问题讨论】:

  • 嗨,马蒂。你有没有解决过这个问题?还是决定改变结构?谢谢
  • @jovanjovanovic Frank 的回答对我有用。

标签: javascript firebase firebase-realtime-database


【解决方案1】:

我猜你正在寻找:

{
  ".read": true,
  ".write": true,
  "rules": {
    "Tasks": {
      "$groupid": {
        ".indexOn": "date"
      }
    }
  }
}

Firebase 文档通常是一本不错的读物。在security guide 上花一个小时或更短的时间,将确保您更好地了解此主题以及许多其他主题。

【讨论】:

  • 我仍然收到“FIREBASE 警告:使用未指定的索引。考虑在 /requests 中将“.indexOn”:“clinics/1/date”添加到您的安全规则中以获得更好的性能“
  • 我在身份验证提供商(例如 google、twitter)和 ID(例如电子邮件)两个级别上遇到了类似的问题。两者我都不能硬编码,我必须通过叶子元素索引什么选项,当两级父级我们不能硬编码时。感谢任何帮助
  • @marty-h $groupid 是提问者要求的通配符条目。
  • 同样的问题,你解决了吗@Steve
【解决方案2】:

我使用了弗兰克的答案,但它给了我错误,

Error saving rules - Line 1: Expected 'rules' property.

所以,这对我有用:

{
  "rules": {
  ".read": true,
  ".write": true,
    "Tasks": {
      "$groupid": {
        ".indexOn": "date"
      }
    }
  }
}

我的数据库规则的屏幕截图:

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2020-08-03
    • 2017-07-19
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多