Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何验证Meteor.methods()中的数据_Meteor - Fatal编程技术网

如何验证Meteor.methods()中的数据

如何验证Meteor.methods()中的数据,meteor,Meteor,我使用Meteor.methods()将数据插入MongoDB,因为我从不信任客户端:如何验证数据(表单输入)服务器端 这样做的最佳方式/实践是什么?提示,提示 范例 Meteor.methods({ addPlayer:函数(formInput){ //验证:如果无效,我将抛出流星。错误。 var playerId=Players.insert({name:formInput.playerName}); 返回playerId; } }); Meteor拥有用于js验证的包匹配 如何从Mete

我使用Meteor.methods()将数据插入MongoDB,因为我从不信任客户端:如何验证数据(表单输入)服务器端

这样做的最佳方式/实践是什么?提示,提示

范例

Meteor.methods({
addPlayer:函数(formInput){
//验证:如果无效,我将抛出流星。错误。
var playerId=Players.insert({name:formInput.playerName});
返回playerId;
}
});

Meteor拥有用于js验证的包
匹配


如何从Meteor.methods()?@MassimilianoMarini中更改错误文本并将错误返回到'Meteor.call('addPlayer',playerName.value,function(Error,result){`?这样做的意义是什么?在客户端调用该方法之前,您应该在客户端上验证(并显示错误)(因此服务器不需要告诉客户端出了什么问题)@PeppeL-G我正考虑将验证任务委托给服务器,并将显示错误消息(如果有的话)的任务委托给客户端。但实际上这没有多大意义。感谢您的澄清。@Peppe L-G并非所有验证规则都可以在客户端上检查,例如验证电子邮件地址是否正忙于其他用户@xhr,你当然是对的(我的评论是关于
check
函数)。
Meteor.methods({addChat: function (roomId, message) {
  check(roomId, String);
  check(message, {
    text: String,
    timestamp: Date,
    // Optional, but if present must be an array of strings.
    tags: Match.Optional([String])
  });