feathersjs / sequelizejs - > MODEL与MODEL

时间:2017-04-11 15:07:01

标签: sequelize.js feathersjs

我在'服务'下面有两个文件夹。夹。他们正在单独调用该服务。现在我想扩展一个模型以包含相关模型,这是一个错误'信息:TypeError:无法读取属性'模型'未定义'

数据库存在,sql server,关系也存在。

文件服务\ mic \ activity \ 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);
};

文件服务\ mic \ activity \ activity-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;
};

文件服务\ mic \ activity \ hooks \ index.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);
}

文件服务\ mic \ 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);
};

文件服务\ mic \ 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;
};

文件服务\ mic \ activity_plan \ hooks \ 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'),现在我得到了活动_计划'与'活动无关。

好的,现在我恢复了我的测试并且拥有这一行model: hook.app.service('/mic/activity_plan').Model。然后我想到了什么。我有自定义主键,所以我需要在hasMany,belongsTo部分中查看

所以现在我制作了第二个数据库,它是通过sequelize(表格)制作的,完全相同的问题。

如果我查看此行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可能会出现问题。< / p>

1 个答案:

答案 0 :(得分:0)

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

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

    sequelize.sync(
        {
            force: false
        }
    );