最近,当我在Sequelize中进行bulkCreate时遇到了一些麻烦。我收到以下错误:
TypeError: Cannot read property 'set' of undefined
at results.forEach (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/lib/model.js:2357:27)
at Array.forEach (<anonymous>)
at QueryInterface.bulkInsert.then.results (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/lib/model.js:2356:21)
at tryCatcher (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/mzd/Desktop/Shroogal/shroogal-dev/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:781:20)
at tryOnImmediate (timers.js:743:5)
at processImmediate [as _immediateCallback] (timers.js:714:5)
这是我的续集迁移文件:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('documentsInstruments', {
docid: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'docid'
},
iid: {
type: DataTypes.BIGINT,
allowNull: true,
references: {
model: 'EntityInstruments',
key: 'IID'
},
field: 'IID'
},
docType: {
type: DataTypes.STRING(45),
allowNull: true,
field: 'docType'
},
docPath: {
type: DataTypes.STRING(200),
allowNull: true,
field: 'docPath'
},
creationDate: {
type: DataTypes.DATE,
allowNull: true,
field: 'creationDate'
},
addlInfo: {
type: DataTypes.STRING(500),
allowNull: true,
field: 'addlInfo'
},
posLabel: {
type: DataTypes.STRING(45),
allowNull: true,
field: 'posLabel'
},
active: {
type: DataTypes.STRING(3),
allowNull: true,
defaultValue: 'Y',
field: 'active'
},
origFileName: {
type: DataTypes.STRING(400),
allowNull: true,
field: 'origFileName'
}
}, {
tableName: 'Documents_Instruments'
});
};
这是我的bulkCreate函数:
if(obj.documents) {
let data = [];
obj.documents.forEach((v, i) => {
var d = {};
if(v.docid)
d.docid = v.docid;
if(obj.iid)
d.iid = obj.iid;
if(v.type)
d.docType = v.type;
if(v.filePath)
d.docPath = v.filePath;
if(v.date)
d.creationDate = v.date;
if(v.additionalDescription)
d.addlInfo = v.additionalDescription;
if(v.originalName)
d.origFileName = v.originalName;
data.push(d);
});
this.documentsInstruments
.bulkCreate(data, {
updateOnDuplicate: ['docType', 'docPath', 'creationDate', 'addlInfo', 'origFileName']
})
.then((d) => {
resolve(d);
})
.catch((err) => {
reject(err);
});
}
我检查源代码(node_modules / sequelize / lib / model.js:2357:27)并尝试打印一些用于调试的东西,实例[i]在此变为unfine.QueryInterface.bulkInsert函数
答案 0 :(得分:0)
我遇到了同样的问题,我也无法找出为什么会出现错误。
但是当我将sequelize库版本从v4.4.2升级到4.11.0时,那个错误就不再显示了。
如果您的版本不是最新版本,请尝试升级版本。