嘿,我是Sails的新手,如果我在模型中将用户名和电子邮件设置为唯一时在提交表单时遇到问题,如果我将数据库中已经存在的用户名和电子邮件放在唯一的用户名中,我只会得到错误,而不是电子邮件,如果我修复了用户名,则我将收到电子邮件错误,但我希望一次显示两个错误
这是模型代码:
username: {
type: "string",
required: true,
unique: true,
},
name: {
type: "string",
required: true,
},
title: {
type: "string",
},
email: {
type: "string",
isEmail: true,
required: true,
unique: true,
},
password: {
type: "string",
minLength: 6
},
这是 Controller 代码
var result=await Users.create(req.allParams(),function(err,data){
if(err)
{
console.log(err);
}
else
{
console.log(data)
}
});```
最佳答案
你的 Controller 应该像
module.exports = {
create: async (req, res) => {
//get your params and body using req.params or req.body
try {
//controller's logic here
let user = await Users.create(data).fetch();
res.status(200)
.send({ user })
} catch (error) {
//catch all errors here
res.status(500)
.send({ err:error })
}
},
关于node.js - 提交表单时捕获Sails中的所有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046252/