为什么验证规则的行为与onDisconnect更新不同(仅)? [英] Why validation rules behave differently (only) with onDisconnect update?

查看:49
本文介绍了为什么验证规则的行为与onDisconnect更新不同(仅)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用firebase进行了一些测试,发现onDisconnect().update()和使用newData.parent()的简单update()在规则验证中的规则差异.

I have made some tests with firebase and saw difference in rules validation between onDisconnect().update() and simple update() using newData.parent() in rules.

当我使用update()时,它可以工作.但是使用onDisonnect().update()我有PERMISSION_DENIED!

When I using update() it works. But with onDisonnect().update() I have PERMISSION_DENIED!

数据库规则:

{
    "rules": {
        "$id": {
            ".read": true,
            ".write": true,
            "a": { ".validate": true },
            "b": { ".validate": "newData.parent().child('a').val() == 'a'" },
        }
    }
}

JavaScript:

Javascript:

const valid = {
  a: 'a',
  b: 'b'
};

function update(obj) {
  console.log('update', obj);
  return ref.update(obj);
}

function onDisconnectUpdate(obj) {
  console.log('onDisconnectUpdate', obj);
  return ref.onDisconnect().update(obj);
}

update(valid);
onDisconnectUpdate(valid);

完整的示例代码

希望你能帮助我.

推荐答案

firebaser此处

onDisconnect()操作期间,当我们验证规则时,每个属性都被视为单独的写操作.这意味着在您的情况下,该操作将失败.尽管这是一个不幸的设计,但在不久的将来不太可能改变.因此,我建议在onDisconnect()处理程序中使用更简单的写入操作.

During an onDisconnect() operation, each property is treated as a separate write operation while we're validating the rules. That means that in your case the operation fails. While this is an unfortunate design, it is unlikely to change in the near future. For this reason I recommend using simpler write operations in onDisconnect() handlers.

这篇关于为什么验证规则的行为与onDisconnect更新不同(仅)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆