Reputation: 59
Currently receiving the error from my notnull validator, however I am passing defined variables through a new object that clearly have values. user, site, and contact are all associations, so im not sure if in my api route if im not to create the properties then just pass them from my post request, however since this is MySql I do believe that it is required, I could most certainly be wrong. Any help or insight would be greatly appreciated.
Api Route
console.log(req.body);
db.Case.create({
caseName: req.body.caseName,
userId: req.body.userId,
siteId: req.body.siteId,
contactId: req.body.contactId
}).then((response) => {
console.log(response);
res.json(response)
}).catch((err) => {
console.log(err);
})
})
Model
module.exports = (sequelize, DataTypes) => {
const Case = sequelize.define('Case', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
caseName: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE,
},
})
Case.associate = (models) => {
Case.belongsTo(models.User, {
foreignKey : {
allowNull : false
}
})
Case.belongsTo(models.Site, {
foreignKey: {
allowNull: false
}
})
Case.belongsTo(models.Contact, {
foreignKey: {
allowNull : false
}
})
}
return Case;
}
post request
const caseName = $('#caseName');
const UserId = sessionStorage.getItem('id');
const SiteId = $('#insertSite').attr('id');
const ContactId = $('#insertContact').attr('id');
if(!caseName.val().trim() || !UserId || !SiteId || !ContactId){
console.log('Please enter all of the Case information')
return;
}
const newCase = {
caseName: caseName.val().trim(),
UserId: UserId,
SiteId: SiteId,
ContactId: ContactId,
};
console.log(newCase);
axios.post('/api/Case', newCase).then((res) => {
console.log('New Case has been added' + res);
}).catch((err) => {
console.log(err);
});
testing object that is being passed
{
caseName: 'tttttttttt',
UserId: '1',
SiteId: 'insertSite',
ContactId: 'insertContact'
}
Error Response
errors: [
ValidationErrorItem {
message: 'Case.UserId cannot be null',
type: 'notNull Violation',
path: 'UserId',
value: null,
origin: 'CORE',
instance: [Case],
validatorKey: 'is_null',
validatorName: null,
validatorArgs: []
},
ValidationErrorItem {
message: 'Case.SiteId cannot be null',
type: 'notNull Violation',
path: 'SiteId',
value: null,
origin: 'CORE',
instance: [Case],
validatorKey: 'is_null',
validatorName: null,
validatorArgs: []
},
ValidationErrorItem {
message: 'Case.ContactId cannot be null',
type: 'notNull Violation',
path: 'ContactId',
value: null,
origin: 'CORE',
instance: [Case],
validatorKey: 'is_null',
validatorName: null,
validatorArgs: []
}
]
}
Upvotes: 0
Views: 2221
Reputation: 961
The properties in your API create method are not capitalized properly.
caseName: req.body.caseName,
userId: req.body.userId,
siteId: req.body.siteId,
contactId: req.body.contactId
Both sides of the : for the 3 foreign keys need to have their capitalization fixed.
Upvotes: 1