I have a Job Model
module.exports = (sequelize, Datatypes) => {
const Jobs = sequelize.define('Jobs', {
jobinput: {
type: Datatypes.STRING
},
typeinput: {
type: Datatypes.STRING
},
jobdescinput: {
type: Datatypes.TEXT('medium')
},
locationinput: {
type: Datatypes.STRING
},
salarymininput: {
type: Datatypes.INTEGER
},
salarymaxinput: {
type: Datatypes.INTEGER
},
seniorityinput: {
type: Datatypes.STRING
},
vacancyinput: {
type: Datatypes.INTEGER
},
bountyinput: {
type: Datatypes.INTEGER
}
})
Jobs.associate = function (models) {
Jobs.belongsTo(models.User)
Jobs.belongsTo(models.Company)
Jobs.belongsToMany(models.JobSkills, { through: models.JobHasSkills, foreignKey: 'JobId' })
}
return Jobs
}
and a JobSkill Model
module.exports = (sequelize, Datatypes) => {
const JobSkills = sequelize.define('JobSkills', {
skill: {
type: Datatypes.STRING,
unique: true
}
})
JobSkills.associate = function (models) {
JobSkills.belongsToMany(models.Jobs, { through: models.JobHasSkills, foreignKey: 'JobSkillId' })
}
return JobSkills
}
and a Many to Many JobHasSkills Model
module.exports = (sequelize, Datatypes) => {
const JobHasSkills = sequelize.define('JobHasSkills', { })
JobHasSkills.associate = function (models) {
JobHasSkills.belongsTo(models.Jobs)
JobHasSkills.belongsTo(models.JobSkills)
}
return JobHasSkills
}
and I was trying to do a create function
async create (req, res) {
try {
await Jobs.create(req.body, { include: { model: JobSkills } })
.then(function (createdObjects) {
res.json(createdObjects)
})
} catch (err) {
res.status(500).send({
error: 'an error has occured while posting'
})
}
}
My Json data that i sent through postman is
{ "jobinput": "Title",
"typeinput": "Part Time",
"jobdescinput": "lorem ipsum",
"locationinput": "California",
"salarymininput": "0",
"salarymaxinput": "1000",
"seniorityinput": "Internship",
"vacancyinput": "1",
"bountyinput": "10",
"skills": ["skill1","skill2","skill3" ]
}
I checked this answer Trying to create an instance and multiple related instances in a many to many relationship
But I was getting the catch(error). This is my first project with node js and sequelize. Can anyone help me on this