2017-04-11 75 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 
     } 
    );