TypeError:Cannot read property 'id' of undefined - nodejs - Sequlize
TypeError:Cannot read property 'id' of undefined - nodejs - Sequlize
我正在尝试将多行保存到数据库中,但一直出现此错误:
TypeError: Cannot read property 'id' of undefined
下面是一些代码实现:
数据库插入调用级别:
createsomething(req, res, module, next){
//...
testQuery.testBulkInsert(model, persistable,
function(result){
if(result.error == ''){
for(i = 0; i < result.res.length; i++){
module.list.push(result.res[i].id);
}
next(module);
} else {
console.log('error #&@{;$÷');
console.log(result.error);
}
}
数据库插入操作级别:
function testBulkInsert(model, values, next){
console.log(values);
model.bulkCreate(values)
.then(function(obj){
let result = new ResponseClass();
result.res = obj;
next(result);
})
.catch(function(err){
console.log('Error during create new row...', err);
let result = new ResponseClass();
result.error = 'Error during create new row...';
next(result);
});
}
回应class:
function SQLResponse(){
this.res = '';
this.error = '';
}
module.exports = SQLResponse;
在 testBulkInsert 函数中 console.log(values);结果很好,我得到了对象:
[
{
title: 'dfasfa',
url: 'fadfa',
someOtherModelId: 18
}, {
title: 'fds',
url: 'ghhgdj',
someOtherModelId: 18
}
]
和模型
var SomeModel= sequelize.define("SomeModel",
{
id : { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false },
title : {type : DataTypes.STRING, allowNull: false },
url : {type : DataTypes.STRING, allowNull: false }
}
);
SomeModel.associate = function(models) {
SomeModel.belongsTo(models.someOtherModelId, {foreignKey: {name:'someOtherModelId', allowNull:false}});
}
有什么想法吗?
------编辑------
我有一些奇怪的行为:
该过程 - 在 testBulkInsert 中 - 还执行 "then" 和 "catch" 分支。第一次保存 - 实际上,对象存在于数据库中 - 元素,然后它移动到 catch 分支并抛出这个错误......我没有在工作流程中两次调用这个方法....
我明白了:只需删除 createsomething 方法中的 for 循环 - 此模型不需要该过程 - 实际上我在 if 语句中也有一些错误代码,这里没有引用什么。我修复了这些,并很好地编码 运行。 :)
我正在尝试将多行保存到数据库中,但一直出现此错误:
TypeError: Cannot read property 'id' of undefined
下面是一些代码实现:
数据库插入调用级别:
createsomething(req, res, module, next){
//...
testQuery.testBulkInsert(model, persistable,
function(result){
if(result.error == ''){
for(i = 0; i < result.res.length; i++){
module.list.push(result.res[i].id);
}
next(module);
} else {
console.log('error #&@{;$÷');
console.log(result.error);
}
}
数据库插入操作级别:
function testBulkInsert(model, values, next){
console.log(values);
model.bulkCreate(values)
.then(function(obj){
let result = new ResponseClass();
result.res = obj;
next(result);
})
.catch(function(err){
console.log('Error during create new row...', err);
let result = new ResponseClass();
result.error = 'Error during create new row...';
next(result);
});
}
回应class:
function SQLResponse(){
this.res = '';
this.error = '';
}
module.exports = SQLResponse;
在 testBulkInsert 函数中 console.log(values);结果很好,我得到了对象:
[
{
title: 'dfasfa',
url: 'fadfa',
someOtherModelId: 18
}, {
title: 'fds',
url: 'ghhgdj',
someOtherModelId: 18
}
]
和模型
var SomeModel= sequelize.define("SomeModel",
{
id : { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false },
title : {type : DataTypes.STRING, allowNull: false },
url : {type : DataTypes.STRING, allowNull: false }
}
);
SomeModel.associate = function(models) {
SomeModel.belongsTo(models.someOtherModelId, {foreignKey: {name:'someOtherModelId', allowNull:false}});
}
有什么想法吗?
------编辑------
我有一些奇怪的行为: 该过程 - 在 testBulkInsert 中 - 还执行 "then" 和 "catch" 分支。第一次保存 - 实际上,对象存在于数据库中 - 元素,然后它移动到 catch 分支并抛出这个错误......我没有在工作流程中两次调用这个方法....
我明白了:只需删除 createsomething 方法中的 for 循环 - 此模型不需要该过程 - 实际上我在 if 语句中也有一些错误代码,这里没有引用什么。我修复了这些,并很好地编码 运行。 :)