2016-03-04 82 views
0

我的代碼有些問題,我找不到它在哪裏。 我在節點服務器上使用Sequelize。 看來我不能讓belongsTo協會工作。Sequelize hasMany協會無法正常工作

這是我自許捕獲錯誤:

[TypeError: Property 'setDetails' of object [object SequelizeInstance:car] is not a function]

其實它的作用是插入詳細的數據,但沒有car_id,它不會插入汽車表中的數據。

這裏就是我:

型號/ detail.js文件:

var Detail = pg.sequelize.define('details', { 
     color: { 
      type: pg.Sequelize.STRING, 
      allowNull: false 
     } 
    }, { 
     timestamps: false, 
     underscored: true 
    } 
); 

module.exports = Detail; 

型號/ car.js

var Detail = require('../models/detail'); 
var Car = pg.sequelize.define('cars', { 
     id: { 
      type: pg.Sequelize.INTEGER, 
      primaryKey: true, 
      autoIncrement: true 
     }, 
     title: { 
      type: pg.Sequelize.STRING, 
      allowNull: false 
     } 
    }, { 
     underscored: true 
    } 
); 
var c = Detail.belongsTo(Car, { 
    as: 'detail', 
    foreignKey: 'car_id', 
    onDelete: 'CASCADE' 
}); 
Car.c = c; 
module.exports = Car; 

路由器/ car.js

var Car = require('../models/car'); 
router.post('/add', function(req, res, next) { 

    var createData = { 
     title: req.body.title, 
     detail: { 
      color: req.body.color 
     } 
    }; 

    Car.create(createData, { include: [ Car.c ] }) 
     .then(function(result) { 
      res.status(200).json(result); 
     }) 
     .catch(function(err) { 
      console.log(err); 
     }); 

}); 

module.exports = router; 

我已經能夠做一個h asMany協會,但不是這一個。

我的代碼有什麼問題?

回答