Cloud Functions for Firebase - 如果值太长,如何停止写入实时数据库
Cloud Functions for Firebase - How to stop writing to realtime database if value is too long
如果 value/string 太长,如何防止写入 firebase 数据库?
例如:如果我有这个数据集
- 用户产品
- 用户名
- 产品编号
- 名字
- 价格
如果 'name' 超过 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;
}
});
或者这段代码完全错误?
使用实时数据库规则进行更快的验证:https://firebase.google.com/docs/database/security/securing-data
在你的情况下,添加这条规则:
".validate": "newData.isString() && newData.val().length < 10"
这应该在您的实时数据库规则中,而不是在您的函数中。这样,用户甚至无法写入您插入规则的路径。
如果 value/string 太长,如何防止写入 firebase 数据库?
例如:如果我有这个数据集
- 用户产品
- 用户名
- 产品编号
- 名字
- 价格
如果 'name' 超过 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;
}
});
或者这段代码完全错误?
使用实时数据库规则进行更快的验证:https://firebase.google.com/docs/database/security/securing-data
在你的情况下,添加这条规则:
".validate": "newData.isString() && newData.val().length < 10"
这应该在您的实时数据库规则中,而不是在您的函数中。这样,用户甚至无法写入您插入规则的路径。