我收到此错误:
code: 'ER_DUP_ENTRY',
errno: 1062,
sqlState: '23000',
index: 0,
sql: 'INSERT INTO `users` (`id`,`name`,`environment_hash`) VALUES (DEFAULT,\'dasxdsvfbw\',\'2ec13352-89cc-4921-806f-22c1f3bdf29c\');' },
sql: 'INSERT INTO `users` (`id`,`name`,`environment_hash`) VALUES (DEFAULT,\'dasxdsvfbw\',\'2ec13352-89cc-4921-806f-22c1f3bdf29c\');' }
我怎样才能解决这个问题?
这是我的模型/user.js
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING,
environment_hash: {
type: DataTypes.STRING,
defaultValue: DataTypes.UUIDV4
}
}, {
tableName: 'users',
underscored: false,
timestamps: false
}
);
return User;
};
这是我的routes.js:
app.post('/signup', function(request, response){
console.log(request.body.email);
console.log(request.body.password);
User
.find({ where: { name: request.body.email } })
.then(function(err, user) {
if (!user) {
console.log('No user has been found.');
User.create({ name: request.body.email }).then(function(user) {
// you can now access the newly created task via the variable task
console.log('success');
}).catch(function(error) {
console.log(error);
});
}
});
});
最佳答案
Sequelize then
回调的第一个参数是查询结果,而不是错误。所有错误都由 catch
回调处理。 Sequelize 查询的语法:
Model.find().then(function(result){
//handling result
}).catch(function(error){
//handling error
});
关于javascript - Sequelize 获取重复条目 : 'ER_DUP_ENTRY' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710047/