Unhandled rejection SequelizeUniqueConstraintError: Validation error

后端 未结 5 603
花落未央
花落未央 2021-02-01 23:47

I\'m getting this error:

Unhandled rejection SequelizeUniqueConstraintError: Validation error

How can I fix this?

This is my models/use

5条回答
  •  生来不讨喜
    2021-02-02 00:24

    The call to User.create() is returning a Promise.reject(), but there is no .catch(err) to handle it. Without catching the error and knowing the input values it's hard to say what the validation error is - the request.body.email could be too long, etc.

    Catch the Promise reject to see the error/validation details

    User.create({ name: request.body.email })
    .then(function(user) {
        // you can now access the newly created user
        console.log('success', user.toJSON());
    })
    .catch(function(err) {
        // print the error details
        console.log(err, request.body.email);
    });
    

    Update, since it's 2019 and you can use async/await

    try {
      const user = await User.create({ name: request.body.email });
      // you can now access the newly created user
      console.log('success', user.toJSON());
    } catch (err) {
      // print the error details
      console.log(err, request.body.email);
    }
    

提交回复
热议问题