2016-05-10 11 views
6

की संपत्ति नहीं पढ़ सकते हैं मैं अब एक घंटे से अधिक के लिए 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 

किसी को भी इन बीज चल के साथ किसी भी अनुभव है?

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

संपादित करें: db से आउटपुट: आपकी जानकारी के लिए यहाँ मेरी कॉन्फ़िग फ़ाइल है विस्थापित

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. 
+0

हाय, क्या आप 'sequelize डीबी: माइग्रेट' का टर्मिनल आउटपुट दिखा सकते हैं? – paolord

+0

@paolord मैंने अपना मूल प्रश्न संपादित किया और डीबी से आउटपुट जोड़ा: नीचे – Tikkes

+0

पर माइग्रेट करें क्या आप डीबी बोली का उपयोग कर रहे हैं? मुझे पोस्टग्रेज़ और केस संवेदनशील नामों के साथ समस्याएं थीं (यानी 'कारों' बनाम 'कारों' तालिका पोस्टग्रेस बनाते हैं) –

उत्तर

0

के बाद से मैं अभी भी नहीं किया है इस काम के प्राप्त करने में सक्षम हो गया है, मैं, अब तो, मेरा समाधान पोस्ट करें जो मैंने उपयोग किया है जो कि अच्छी तरह से काम करता है लेकिन कस्टम बनाया गया है।

सबसे पहले, मैंने जेएसओएन फाइल बनाई है जिसमें ऑब्जेक्ट है जिसे मैं बीज बनाना चाहता हूं। यानी

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

इसके बाद, मैं

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 
} 

अब यह सब सेटअप पूरा हो जाने अपने आवेदन निम्नलिखित (बेशक छंटनी संस्करण) युक्त के सर्वर साइड में एक seeder.js लागू कर दिया है, मैं इस का उपयोग कर सकते 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); 
} 

यह है कि, अब, जब आप अपना आवेदन चलाएं, यदि आपके किसी भी ऑब्जेक्ट को डेटाबेस में पहले से डाला गया है तो बीडर डेटाबेस की जांच करेगा। यदि ऐसा है, तो बीडर छोड़ दिया जाता है, यदि नहीं, तो वे डाले जाते हैं।

मुझे आशा है कि यह आपको लोगों की मदद करेगा।

संबंधित मुद्दे