Sequelize-CLI Seeders – 无法读取未定义的属性

我一直在用db:seed:all挣扎db:seed:all的一个多小时,现在慢慢地,我对此失去了信心。

我有一个简单的模型:

 'use strict'; module.exports = function (sequelize, DataTypes) { var Car = sequelize.define('Cars', { name: DataTypes.STRING, type: DataTypes.INTEGER, models: DataTypes.INTEGER }, { classMethods: { associate: function (models) { // associations can be defined here } } }); return Car; }; 

这是在迁移,并使用sequelize db:migrate到数据库,它工作正常。

接下来,我想插入 – 通过种子文件 – 2辆车。 所以我运行了命令sequelize seed:create --name insertCars并添加了bulkInsert

 'use strict'; module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.bulkInsert( 'Cars', [ { name: "Auris", type: 1, models: 500, createdAt: Date.now(), updatedAt: Date.now() }, { name: "Yaris", type: 1, models: 500, createdAt: Date.now(), updatedAt: Date.now() } ] ); }, down: function (queryInterface, Sequelize) { } }; 

现在当我运行sequelize db:seed:all我得到以下错误:

 Loaded configuration file "config\config.json". Using environment "development". == 20160510132128-insertCars: migrating ======= Seed file failed with error: Cannot read property 'name' of undefined 

有没有人有运行这些播种机的经验? 为了您的信息,这里是我的configuration文件:

 { "development": { "username": "mydbdude", "password": "mydbdude", "database": "Cars", "host": "127.0.0.1", "dialect": "mssql", "development": { "autoMigrateOldSchema": true } }, ....other configs } 

编辑:输出从数据库:迁移

 Sequelize [Node: 5.9.1, CLI: 2.4.0, ORM: 3.23.0] Loaded configuration file "config\config.json". Using environment "development". No migrations were executed, database schema was already up to date. 

由于我还没有能够得到这个工作,现在,我发布我的解决scheme,我使用哪个工作相当好,但是是定制的。

首先,我创build了一个包含我想播种的对象的JSON文件。 即

 dataSources: [ { name: 'Pattern' }, { name: 'Upload' } ] 

接下来,我已经在我的应用程序的服务器端实现了一个seeder.js ,其中包含以下内容(修剪版本的课程)

 var models = require('./../models'), sql = models.sequelize, Promise = models.Sequelize.Promise; var objects = require('./seed/objects'), //this is where my object file is dataSources = objects.dataSources; var express = require('express'); var seedDatabase = function () { var promises = []; promises.push(createDataSources()); //more can be added to the promises return Promise.all(promises); }; function createDataSources() { return models.sequelize .transaction( { isolationLevel: models.sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED }, function (t) { return models.DataSource .findAll({ attributes: [ 'id' ] }) .then(function (result) { if (!result || result.length == 0) { return models.DataSource .bulkCreate(dataSources, { transaction: t }) .then(function () { console.log("DataSources created"); }) } else { console.log("DataSources seeder skipped, already objects in the database..."); return; } }); }) .then(function (result) { console.log("DataSources seeder finished..."); return; }) .catch(function (error) { console.log("DataSources seeder exited with error: " + error.message); return; }); }; module.exports = { seedDatabase: seedDatabase } 

现在所有这些设置完成了,我可以在我的应用程序启动时使用这个seedDatabase函数来运行我的播种机,如下所示:

 //includes and routes above, not interesting for this answer try { umzug.up().then(function (migrations) { for (var i = 0; i < migrations.length; i++) { console.log("Migration executed: " + migrations[i].file); } console.log("Running seeder"); seeder.seedDatabase().then(function () { //here I run my seeders app.listen(app.get('port'), function () { console.log('Express server listening on port ' + app.get('port')); }); }); }); } catch (e) { console.error(e); } 

就是这样,现在,当你运行你的应用程序时,播种器将检查数据库是否有任何对象已经插入到数据库中。 如果是这样,则播种器被跳过,否则插入播种器。

我希望这会帮助你们。