gpt4 book ai didi

sails.js - "email"验证规则崩溃 Sails 服务器 - 带有 Sails.js 的 Mongo

转载 作者:行者123 更新时间:2023-12-04 11:04:35 28 4
gpt4 key购买 nike

当sails.js 模块上的电子邮件验证规则失败时,服务器正在崩溃。
这是我的模块的片段:
//用户的电子邮件地址

email: {
type: 'string',
email: true,
required: true,
unique: true
},

错误如下:

err: Error (E_VALIDATION) :: 1 attribute is invalid at WLValidationError.WLError (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLError.js:26:15) at new WLValidationError (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLValidationError.js:20:28) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\query\validate.js:45:43 at allValidationsChecked (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:203:5) at done (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:135:19) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:32:16 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:184:23 at done (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:135:19) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:32:16 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:157:64 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:125:13 at Array.forEach (native) at _each (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:46:24) at Object.async.each (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:124:9) at validate (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:156:11) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:125:13 Invalid attributes sent to User: • email • undefined should be a email (instead of "admin@gmailasd", which is a string)

最佳答案

声明电子邮件字段的正确方法是这样的:

email: {
type: 'email',
required: true,//Email field will be required for insert or update
unique: true //Insert or update will crash if you try to insert duplicate email
},

您可以在此处查看所有不同的属性类型 http://sailsjs.org/documentation/concepts/models-and-orm/attributes

如果您想捕获插入/更新错误,您可以在 Controller 上执行此操作:
MyModel.create({email:email}).exec(function(err, model)
{
if(err)
{
//Check if it's a validation error or a crash
if(err.code == "E_VALIDATION")
sails.log.debug("valid fail, check form");
else
sails.log.debug("crash");
}
else
{
//do what you want to do with the data
}
});

关于sails.js - "email"验证规则崩溃 Sails 服务器 - 带有 Sails.js 的 Mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675698/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com