Sails返回“冲突”,试图拦截唯一的约束错误

时间:2018-07-08 08:29:45

标签: sails.js

我的响应返回“冲突”,试图注册用户。我还确保用户模型在电子邮件地址字段中具有唯一密钥。创建用户后,如何正确拦截唯一约束错误?

module.exports = {
  friendlyName: 'Signup',
  description: 'Signup user.',

  inputs: {
    emailAddress: {
      description: 'The email of the user to sign up',
      type: 'string',
      required: true,
      unique: true,
      isEmail: true
    },
    password: {
      description: 'The password of the user to sign up',
      type: 'string',
      required: true,
      minLength: 6
    }
  },

  exits: {
    emailAddressInUse: {
      statusCode: 409,
      description: 'The provided email address is already in use.'
    }
  },

  fn: async function(inputs, exits) {
    let user = await User.create(inputs)
      .intercept('E_UNIQUE', () => 'emailAddressInUse')
      .fetch();

    return exits.success({
      user
    });
  }
};

0 个答案:

没有答案