我正在学习firebase我有以下简单的规则设置:
{
"rules": {
".read": "auth != null",
"messages": {
".read": "true",
"$conversation_id": {
".write": "newData.hasChildren(['receiver_id','sender_id','timestamp'])"
}
}
}
}
在模拟器中,写规则通过:
{
"receiver_id":123,
"sender_id":"test",
"timestamp":123
}
使用angularFire我得到权限被拒绝(定义了uid):
var dat = {
sender_id:uid,
receiver_id: uid,
timestamp: firebase.database.ServerValue.TIMESTAMP
};
database.ref('messages/myMessageId').push(dat);
如果我删除了hasChildren检查,则会在填充了正确值的情况下将消息添加到数据库中。是什么给了什么?
答案 0 :(得分:1)
Firebase.push()
会自动创建一个新的唯一ID(类似于数组索引),因此您应该让AngularFire使用database.ref('messages').push(dat)
// will fail because /myMessageId does not contain child sender_id
database.ref('messages/myMessageId').push(dat);
{
messages: {
myMessageId: {
UID: {
sender_id: 1,
...
}
}
}
}
// will work
database.ref('messages').push(dat);
{
messages: {
UID: {
sender_id: 1,
...
}
}
}