gpt4 book ai didi

node.js - Sequelize-CLI 播种机 - 无法读取未定义的属性

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:20 26 4
gpt4 key购买 nike

我已经为 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: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.

最佳答案

由于我仍然无法使它正常工作,所以我现在发布我使用的解决方案,该解决方案运行良好但是定制的。

首先,我创建了一个包含我想要播种的对象的 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);
}

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

希望对大家有所帮助。

关于node.js - Sequelize-CLI 播种机 - 无法读取未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com