2016-11-20 67 views
0

我得到這個錯誤:Sequelize越來越重複的條目: 'ER_DUP_ENTRY'

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

       } 
      }); 



    }); 
+0

如何創建初始數據庫模式?通過續集,還是手動? – drinchev

回答

0

Sequelize的第一個參數then回調查詢,而不是錯誤的結果。所有錯誤都由catch回調處理。 Sequelize查詢語法:

Model.find().then(function(result){ 
    //handling result 
}).catch(function(error){ 
    //handling error 
});