【问题标题】:Cloud Functions for Firebase - How to stop writing to realtime database if value is too longCloud Functions for Firebase - 如果值太长,如何停止写入实时数据库
【发布时间】:2018-01-17 13:33:51
【问题描述】:

如果值/字符串太长,如何防止写入 firebase 数据库?

例如:如果我有这个数据集

  • 用户产品
    • 用户ID
    • 产品ID
      • 姓名
      • 价格

如果“名称”长度超过 10 个字符,我不想将此产品添加到数据库中。我该怎么做?

像这样:?

exports.preventNameTooLong = functions.database.ref('/userProducts/{userID}/')
.onWrite(event => {
    // Grab the current value of what was written to the Realtime Database.
    const product = event.data.val();
    if(product.name.length > 10){
        return;
    }else{
        return event.data.ref;
    }
});

还是这段代码完全错误?

【问题讨论】:

    标签: firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    使用实时数据库规则加快验证速度:https://firebase.google.com/docs/database/security/securing-data

    在你的情况下,添加这条规则:

    ".validate": "newData.isString() && newData.val().length < 10"
    

    这应该在您的实时数据库规则中,而不是在您的函数中。这样,用户甚至无法写入您插入规则的路径。

    【讨论】:

    • Ofc :D 感谢您的回答!
    • 抱歉回复晚了☺️
    猜你喜欢
    • 2017-09-14
    • 2017-11-12
    • 2017-10-26
    • 2020-04-25
    • 1970-01-01
    • 2018-05-31
    • 2019-10-03
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多