Sequelize-CLI Seeders - Unable to read undefined property

I’ve been struggling with db:seed:all for more than an hour and am slowly losing my mind about it.

I have a simple model:

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

it is in the process of migration and goes to the database using sequelize db:migrate , which works fine.

Then I wanted to insert - through the seed file - 2 machines. So I sequelize seed:create --name insertCars and added 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) { } }; 

Now when I run sequelize db:seed:all , I get the following error:

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

Does anyone have experience with these seeders? For your information, here is my configuration file:

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

EDIT: output from db: migrate

 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. 
+6
source share
1 answer

Since I still have not been able to get this work, I will now submit my solution, which I used, which works pretty well, but is custom-made.

First I created a JSON file containing the object I want to sow. i.e.

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

Then I applied seeder.js on the server side of my application containing the following (cropped version, of course)

 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 } 

Now all this setup is done, I can use this seedDatabase function when my application starts to start my seeder, for example:

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

Now, when you start the application, the seeder will check the database if any of your objects are already inserted into the database. If so, the seeder is skipped; if not, they are inserted.

Hope this helps you guys.

0
source

All Articles