2017-04-11 80 views
0

我有两个文件夹下的'服务'文件夹。他们正在单独调用该服务。现在我想扩展一个模型,包括相关的模型,这给了一个错误'信息:TypeError:无法读取属性未定义的'模型'feathersjs/sequelizejs - > MODEL没有关联模型

数据库存在,sql服务器,以及关系也存在。

文件服务\麦克风\活动\ index.js

'use strict'; 

const service = require('feathers-sequelize'); 
const activity = require('./activity-model'); 
const hooks = require('./hooks/index'); 

module.exports = function() { 
    const app = this; 

    const options = { 
     Model: activity(app.get('mic_sequelize')) 
    }; 

    app.use('/mic/activity', service(options)); 

    const activityService = app.service('/mic/activity'); 

    activityService.before(hooks.before); 
    activityService.after(hooks.after); 
}; 

文件服务\麦克风\活动\活动的model.js

'use strict'; 

const Sequelize = require('sequelize'); 

module.exports = function (sequelize) { 
    const activity = sequelize.define('activity', { 
     activity_pk: { 
      type: Sequelize.INTEGER, 
      primaryKey: true, 
      autoIncrement: true, 
      allowNull: false, 
      unique: true 
     }, 
     activity_plan_pk: { 
      type: Sequelize.INTEGER, 
      allowNull: false, 
      unique: false 
     }, 
     change_order_pk: { 
      type: Sequelize.INTEGER, 
      allowNull: true, 
      unique: false 
     }, 
     description: { 
      type: Sequelize.STRING(255), 
      allowNull: false, 
      unique: false 
     }, 
     id: { 
      type: Sequelize.STRING(255), 
      allowNull: false, 
      unique: true 
     } 
    }, { 
     freezeTableName: true, 
     paranoid: false, 
     timestamps : false, 
     underscored : false, 
     tableName: 'Activity', 
     classMethods: { 
      associate() { 
       activity.belongsTo(sequelize.models.activity_plan, { 
        allowNull: true, 
        as: 'activity_plan' 
       }); 
       activity.belongsTo(sequelize.models.change_order, { 
        allowNull: false, 
        as: 'change_order' 
       }); 
      } 
     } 
    }); 

    return activity; 
}; 

文件服务\麦克风\活动\吊钩\指标.js文件

'use strict'; 

const globalHooks = require('../../../../hooks'); 
const hooks = require('feathers-hooks'); 
const auth = require('feathers-authentication').hooks; 

exports.before = { 
    all: [], 
    find: [ 
     getRelatedInfo() 
    ], 
    get: [ 
     getRelatedInfo() 
    ], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
}; 

exports.after = { 
    all: [], 
    find: [], 
    get: [], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
}; 


function getRelatedInfo() { 
    return function (hook) { 
     hook.params.sequelize = { 
      attributes: [ 
       'activity_pk', 
       'activity_plan_pk', 
       'change_order_pk', 
       ['description', 'activity_description'], 
       ['id', 'activity_id'] 
      ], 
      include: [ 
       { 
        model: hook.app.services.activity_plan.Model, 
        as: 'activity_plan', 
        required: false, 
        attributes: [ 
         ['description', 'activity_plan_description'], 
         ['id', 'activity_plan_id'] 
        ] 
       } 
      ] 
     } 
    }; 
    return Promise.resolve(hook); 
} 

文件服务\麦克风\ activity_plan \ index.js

'use strict'; 

const service = require('feathers-sequelize'); 
const activity_plan = require('./activity_plan-model'); 
const hooks = require('./hooks/index'); 

module.exports = function() { 
    const app = this; 

    const options = { 
     Model: activity_plan(app.get('mic_sequelize')) 
    }; 

    app.use('/mic/activity_plan', service(options)); 

    const activityplanService = app.service('/mic/activity_plan'); 

    activityplanService.before(hooks.before); 
    activityplanService.after(hooks.after); 
}; 

文件服务\麦克风\ activity_plan \ activity_plan-model.js

'use strict'; 

const Sequelize = require('sequelize'); 

module.exports = function (sequelize) { 
    const activity_plan = sequelize.define('activity_plan', { 
     activity_plan_pk: { 
      type: Sequelize.INTEGER, 
      primaryKey: true, 
      autoIncrement: true, 
      allowNull: false, 
      unique: true 
     }, 
     description: { 
      type: Sequelize.STRING(255), 
      allowNull: false, 
      unique: false 
     }, 
     id: { 
      type: Sequelize.STRING(255), 
      allowNull: false, 
      unique: true 
     } 
    }, { 
     freezeTableName: true, 
     paranoid: false, 
     timestamps : false, 
     underscored : false, 
     tableName: 'Activity_Plan', 
     classMethods: { 
      associate() { 
       activity_plan.hasMany(sequelize.models.activity, { 
        as: 'activity_plan', 
        foreignKey: 'activity_plan_pk', 
        targetKey: 'activity_plan_pk' 
       }); 
      } 
     } 
    }); 

    return activity_plan; 
}; 

文件服务\麦克风\ activity_plan \吊钩\ index.js

'use strict'; 

const globalHooks = require('../../../../hooks'); 
const hooks = require('feathers-hooks'); 
const auth = require('feathers-authentication').hooks; 

exports.before = { 
    all: [], 
    find: [], 
    get: [], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
}; 

exports.after = { 
    all: [], 
    find: [], 
    get: [], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
}; 

问题

我认为我有一个配置错误的地方。我使用了一个我自己的工作示例应用程序,其中服务没有子文件夹,并且关系采用相同的方式。这是行得通的。任何想法的问题是什么?

解决

好。所以我将activity和activity_Plan移回到服务的根目录,而不改变除文件位置之外的任何内容。同样的错误。我改变了服务的路径(删除了'/ mic'),现在我得到的'activity_plan'与'activity'没有关联。

好吧,现在我恢复了我的测试,并有这条线model: hook.app.service('/mic/activity_plan').Model。然后我想到了一些事情。我有自定义主键,所以我需要做一些在hasMany,belongsTo部分调查

因此,现在我做了第二个数据库,通过sequelize(表)strangly,完全相同的问题。

如果我看这条线at Model.validateIncludedElement (D:\Workspace\Projects\01. Live Customers\Mexon Technology\Projects - Mexon\MexonInControl\MultiFunctionalPortal\backend\node_modules\sequelize\lib\model.js:558:11),然后在它指向的代码中,这可能是我的模型命名与我的代码中的行model: hook.app.service('/mfp/sys_metadata').Model组合在一起的问题。

回答

0

哦,亲爱的。我完全忘了初始化服务子文件夹的index.js文件中的关联:

Object.keys(sequelize.models).forEach(function(modelName) { 
     if ("associate" in sequelize.models[modelName]) { 
      sequelize.models[modelName].associate(); 
     } 
    }); 

    sequelize.sync(
     { 
      force: false 
     } 
    );