私は後発のアソシエーションを1対多に取り組んでいます。ほとんどのチュートリアルとドキュメンテーションは、両方のモデルが同じファイルに定義されている場合の例を示しています。 私は現在、2つのファイル、最初city.js持っている:私は両方のモジュールをインポートして、オブジェクトをインスタンス化しようとすると複数のファイルで1対多数のアサーションを続ける
const Promise = require('bluebird');
var City = require('./city');
var Country = sequelize.define("Country", {
id: {
type: DataTypes.INTEGER,
field: 'id',
primaryKey: true,
autoIncrement: true
},
...
}, {
freezeTableName: true,
timestamps: false,
paranoid: false
});
Country.hasMany(City, {foreignKey : 'countryId', as: 'Cities'});
Promise.promisifyAll(Country);
module.exports = Country;
:
const Promise = require('bluebird');
var Country = require('./country');
var City = sequelize.define("City", {
id: {
type: DataTypes.INTEGER,
field: 'id',
primaryKey: true,
autoIncrement: true
},...
}, {
freezeTableName: true,
timestamps: false
});
City.belongsTo(Country, {foreignKey : 'countryId', as: 'Country'});
Promise.promisifyAll(City);
module.exports = City;
そして、第二のファイルcountry.jsを
var City = require('../model/external/city');
var CountryRepository = require('../repository/external/countryRepository');
CountryRepository.findById(1).then(function(country) {
var city = City.build();
city.name = 'Paris';
city.setCountry(country);
console.log('OK');
});
次のエラーが発生します。
throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not an instance of Sequelize.Model')
モデルからエクスポートされる前にモデルがプロミス化されているか、何か不足していますか?
ありがとうございました。少しのindex.js変更で、私は望みの結果を得ました –