Sequelize associations doesn't work
Sequelize associations doesn't work
i've got this two models
module.exports = function (sequelize, DataTypes)
return sequelize.define('transportationType',
description:
type: DataTypes.STRING,
allowNull : false
)
module.exports = function (sequelize, DataTypes)
return sequelize.define('Transportation',
code: DataTypes.INTEGER,
description: DataTypes.STRING,
seatQty: DataTypes.INTEGER
)
and here is the association
db.Transportation.belongsTo(db.TransportationType);
and i make an api like this
var code = parseInt(req.body.code);
var desc = req.body.desc;
var seat = parseInt(req.body.seat);
var type = parseInt(req.body.TransportationType);
db.TransportationType.findById(type)
.then(snap =>
db.Transportation.setTransportationType(snap);
db.Transportation.create(
code: code,
description: desc,
seatQty: seat
)
.then(data => console.log(data))
.catch(err => res.json(err));
)
when i run the code with postman , the console said
db.Transportation.setTransportationType is not a function.
db.Transportation.setTransportationType is not a function.
I'm sure the docs say it will run, but it's not
setTransportationType
Transportation
.setTransportationType
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I believe
setTransportationType
exist on the instance of the model, not on the model class itself. You need to create theTransportation
, then call.setTransportationType
on the created instance.– Aᴍɪʀ
2 days ago