sequelize BulkCreate不能读取未定义的属性“集合”。

时间:2021-05-03 20:09:16

Recently I met some trouble when I was doing bulkCreate in Sequelize. I got the following error:

最近,我在使用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)

And this is my sequelize migration file:

这是我的sequelize迁移文件:

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'
});
};

And this is my bulkCreate function:

这是我的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);
               });

        }

I check the source code(node_modules/sequelize/lib/model.js:2357:27) and try to print something for debugging, the instance[i] become undefine in this.QueryInterface.bulkInsert functions

我检查源代码(node_modules/sequelize/lib/model.js:2357:27),并尝试打印一些用于调试的东西,实例[I]在this.QueryInterface中变成undefine。bulkInsert功能

1 个解决方案

#1


0  

I got same issue you had and I couldn't find out why that error raised too.

我遇到了同样的问题,我也不知道为什么会有这样的错误。

But When i upgrade sequelize library version from v4.4.2 to 4.11.0, and then that error doesn't show up anymore.

但是当我将sequelize库版本从v4.4.2升级到4.11.0时,这个错误就不会再出现了。

If your version is not latest, try upgrade version.

如果您的版本不是最新的,请尝试升级版本。

#1


0  

I got same issue you had and I couldn't find out why that error raised too.

我遇到了同样的问题,我也不知道为什么会有这样的错误。

But When i upgrade sequelize library version from v4.4.2 to 4.11.0, and then that error doesn't show up anymore.

但是当我将sequelize库版本从v4.4.2升级到4.11.0时,这个错误就不会再出现了。

If your version is not latest, try upgrade version.

如果您的版本不是最新的,请尝试升级版本。