如果模型(任务)尚未加载?(在这种情况下,T出现在U之前并加载模型).
Object.keys(db).forEach(function(modelName) { if ("associate" in db[modelName]) { db[modelName].associate(db); } });
变
Object.keys(db).forEach(function(modelName) { if ("associate" in db[modelName]) { db[modelName].associate(db); } }); Object.keys(db).forEach(function(modelName) { if ("loadScopes" in db[modelName]) { db[modelName].loadScopes(db); } });
和模型
module.exports = function(sequelize, DataTypes) { var User = sequelize.define("User", { username: DataTypes.STRING }, { classMethods: { associate: function(models) { User.hasMany(models.Task) } } }); return User; };
变
module.exports = function(sequelize, DataTypes) { var User = sequelize.define("User", { username: DataTypes.STRING }, { classMethods: { associate: function(models) { User.hasMany(models.Task) } loadScopes: function(models) { User.addScope('complete', { include: [{ model: models.Task }] }) } } }); return User; };