【问题标题】:Catch all errors in Sails while submitting form提交表单时捕获 Sails 中的所有错误
【发布时间】:2019-11-26 08:03:11
【问题描述】:

嘿,我是 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
        },

这是控制器代码

var result=await Users.create(req.allParams(),function(err,data){
           if(err)
           {
                 console.log(err);
           }
           else
           {
                 console.log(data)
           }

        });```

【问题讨论】:

  • 你遇到了什么错误?

标签: node.js validation error-handling sails.js


【解决方案1】:

你的控制器应该是这样的

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 })
    }
  },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多