即使传递的对象不为空,Sequelize Not Null 错误
Sequelize Not Null Error even though the object being passed isnt null
目前从我的 notnull 验证器收到错误,但是我正在通过一个显然具有值的新对象传递定义的变量。用户、站点和联系人都是关联,所以我不确定在我的 api 路由中是否不创建属性然后从我的 post 请求中传递它们,但是因为这是 MySql 我相信这是必需的,我肯定是错的。任何帮助或见解将不胜感激。
Api路线
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);
})
})
型号
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请求
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);
});
正在传递的测试对象
{
caseName: 'tttttttttt',
UserId: '1',
SiteId: 'insertSite',
ContactId: 'insertContact'
}
错误响应
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: []
}
]
}
您的 API create 方法中的属性大小写不正确。
caseName: req.body.caseName,
userId: req.body.userId,
siteId: req.body.siteId,
contactId: req.body.contactId
: 的两边,因为 3 个外键需要固定大小写。
目前从我的 notnull 验证器收到错误,但是我正在通过一个显然具有值的新对象传递定义的变量。用户、站点和联系人都是关联,所以我不确定在我的 api 路由中是否不创建属性然后从我的 post 请求中传递它们,但是因为这是 MySql 我相信这是必需的,我肯定是错的。任何帮助或见解将不胜感激。
Api路线
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);
})
})
型号
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请求
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);
});
正在传递的测试对象
{
caseName: 'tttttttttt',
UserId: '1',
SiteId: 'insertSite',
ContactId: 'insertContact'
}
错误响应
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: []
}
]
}
您的 API create 方法中的属性大小写不正确。
caseName: req.body.caseName,
userId: req.body.userId,
siteId: req.body.siteId,
contactId: req.body.contactId
: 的两边,因为 3 个外键需要固定大小写。