我在“服务”文件夹下有两个文件夹。他们正在单独打电话给服务部门。现在,我希望扩展一个模型以包括相关的模型,这会给出一个错误'info: TypeError:无法读取属性‘模型’的未定义‘
数据库、sql服务器以及它们之间的关系也是存在的。
文件services\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\活动\活动-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;
};文件services\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);
}文件services\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);
};文件services\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;
};文件service\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_Plan移回服务的根目录,除了文件位置之外,没有更改任何内容。同样的错误。THen我更改了服务的路径(删除了‘/麦克风’),现在我得到'activity_plan‘与'activity’没有关联。
好了,现在我恢复了我的测试,并有了这一行model: hook.app.service('/mic/activity_plan').Model。然后我想到了一些事情。我有自定义的主键,所以我需要在hasMany,belongsTo部件中做一些研究。
所以现在我创建了第二个数据库,它是通过对(表)的续集来创建的,奇怪的,完全相同的问题。
如果我查看这一行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相结合的一个问题。
发布于 2017-04-12 09:46:40
哦,亲爱的。我完全忘记初始化服务子文件夹的index.js文件中的关联:
Object.keys(sequelize.models).forEach(function(modelName) {
if ("associate" in sequelize.models[modelName]) {
sequelize.models[modelName].associate();
}
});
sequelize.sync(
{
force: false
}
);https://stackoverflow.com/questions/43349741
复制相似问题