Sequelize findOrCreate给我一个SequelizeUniqueConstraintError:验证错误

时间:2016-02-07 00:07:01

标签: node.js database validation sequelize.js

所以我试图将多个续集请求链接在一起 - 检查我们的数据库中是否存在授权。但是,当我使用sequelize findOrCreate方法时,我得到一个SequelizeUniqueConstraintError:验证错误,我无法弄清楚原因。

这是我的基金模式。

module.exports = function(sequelize, DataTypes) {
  var Fund = sequelize.define("funds", {
    id: {
      type: DataTypes.INTEGER,
      field: 'id',
      primaryKey: true,
      unique: true,
      autoIncrement: true,
    },
    title: {
      type: DataTypes.TEXT,
      field: 'title'
    },
    tags: {
      type: DataTypes.ARRAY(DataTypes.TEXT),
      field: 'tags'
    },
    minimum_age: {
      type: DataTypes.INTEGER,
      field: 'minimum_age'
    },
    maximum_age: {
      type: DataTypes.INTEGER,
      field: 'maximum_age'
    },
    minimum_amount: {
      type: DataTypes.INTEGER,
      field: 'minimum_amount'
    },
    maximum_amount: {
      type: DataTypes.INTEGER,
      field: 'maximum_amount'
    },
    invite_only: {
      type: DataTypes.BOOLEAN,
      field: 'invite_only'
    },
    created_at: {
      type: DataTypes.DATE,
      field: 'created_at',
      defaultValue: sequelize.fn('NOW')
    },
    updated_at: {
      type: DataTypes.DATE,
      field: 'updated_at',
      defaultValue: sequelize.fn('NOW')
    },
    deadline: {
      type: DataTypes.DATE,
      field: 'deadline',
    },
    link: {
      type: DataTypes.TEXT,
      field: 'link'
    },
    description: {
      type: DataTypes.TEXT,
      field: 'description'
    },
    countries: {
      type: DataTypes.ARRAY(DataTypes.TEXT),
      field: 'countries'
    },
    religion: {
      type: DataTypes.ARRAY(DataTypes.TEXT),
      field: 'religion'
    },
    gender: {
      type: DataTypes.STRING(40),
      field: 'gender'
    },
    financial_situation: {
      type: DataTypes.TEXT,
  field: 'financial_situation'
},
email: {
  type: DataTypes.STRING(40),
  field: 'email'
},
merit_or_finance: {
  type: DataTypes.TEXT,
  field: 'merit_or_finance'
},
charity_number:{
  type: DataTypes.INTEGER,
  field: 'charity_number'
}
}, {
    timestamps: true,
    underscored: true,
    paranoid: true


});

 return Fund;
};

这是我的控制器功能

fundProfile: function(req, res){
 var fundId = req.params.id;
 models.users.findById(fundId).then(function(user){
  var scholarshipName = user.username;
  var email = user.email;
  //see if fund already exists on fund table
  models.funds.findOrCreate({where:{title: scholarshipName}, defaults:{email: email}}).spread(function(user, created){
    console.log("EVEN HERE", created);
    if(created){
      console.log("LOOK AT ME", user);
      models.users.find({where: {fund_or_user: user.id}}).then(function(user){
        res.render('signup/new-fund-profile', {user: user});
      })
    }

    else{

      var fundTableId = user.id;
      user.update({
        email: email
      }).then(function(user){
        models.users.findById(fundId).then(function(user){
          user.update({
            fund_or_user: fundTableId
          }).then(function(user){
            res.render('signup/new-fund-profile', {user: user});
          })
        })
      })
    }
});
}

非常感谢您的帮助

@edit

This is the error shown
 Unhandled rejection SequelizeUniqueConstraintError: Validation error
at Query.formatError      (/Users/androswong/startup/silofunds/node_modules/sequelize/lib/dialects/postgres/query.js:326:16)
at /Users/androswong/startup/silofunds/node_modules/sequelize/lib/dialects/postgres/query.js:121:24
at tryCatcher (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/util.js:11:23)
at Promise._settlePromiseFromHandler (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:488:35)
at Promise._settlePromise (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:548:18)
at Promise._settlePromise0 (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:593:10)
at Promise._settlePromises (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:676:18)
at Promise._fulfill (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:617:18)
at PromiseArray._resolve (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise_array.js:125:19)
at PromiseArray._promiseFulfilled (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise_array.js:143:14)
at PromiseArray._iterate (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise_array.js:113:31)
at PromiseArray.init [as _init] (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise_array.js:77:10)
at Promise._settlePromise (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:545:21)
at Promise._settlePromise0 (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:593:10)
at Promise._settlePromises (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:676:18)
at Async._drainQueue (/Users/androswong/startup/silofunds/node_modules/sequelize/node_modules/bluebird/js/release/async.js:125:16)

0 个答案:

没有答案