我正在使用带有异议ORM的feathers js 4,并尝试创建一个返回异议js模型数据的服务,该数据包括一个渴望加载的BleongsToOneRelation,但我一直得到响应:
无法读取未定义的属性'get'
我已经使用Feathers CLI生成我的服务(和模型),然后进行了修改,以添加关系,如下所示:
devices.model.js
const { Model } = require('objection');
class devices extends Model {
static get tableName() {
return 'devices';
}
static get jsonSchema() {
return {
type: 'object',
required: ['macAddress'],
properties: {
macAddress: { type: 'string' },
circuitId: { type: 'integer' },
}
};
}
static get relationMappings() {
const Circuit = require('./circuits.model')();
const DeviceType = require('./device-types.model')();
return {
circuit: {
relation: Model.BelongsToOneRelation,
modelClass: Circuit,
join: {
from: 'devices.circuitId',
to: 'circuits.id'
}
}
};
}
$beforeInsert() {
this.createdAt = this.updatedAt = new Date().toISOString();
}
$beforeUpdate() {
this.updatedAt = new Date().toISOString();
}
}
module.exports = function (app) {
const db = app.get('knex');
db.schema.hasTable('devices').then(exists => {
if (!exists) {
db.schema.createTable('devices', table => {
table.increments('id');
table.string('macAddress');
table.integer('circuitId');
table.timestamp('createdAt');
table.timestamp('updatedAt');
})
.then(() => console.log('Created devices table')) // eslint-disable-line no-console
.catch(e => console.error('Error creating devices table', e)); // eslint-disable-line no-console
}
})
.catch(e => console.error('Error creating devices table', e)); // eslint-disable-line no-console
return devices;
};
device.class.js
const { Devices } = require('./devices.class');
const createModel = require('../../models/devices.model');
const hooks = require('./devices.hooks');
module.exports = function (app) {
const options = {
Model: createModel(app),
paginate: app.get('paginate'),
whitelist: ['$eager', '$joinRelation', '$modifyEager'],
allowedEager: ['circuit']
};
// Initialize our service with any options it requires
app.use('/devices', new Devices(options, app));
// Get our initialized service so that we can register hooks
const service = app.service('devices');
service.hooks(hooks);
};
然后我使用http://localhost:3030/devices?$ eager = circuit调用/ devices GET请求>
结果是:
{ "name": "GeneralError", "message": "Cannot read property 'get' of undefined", "code": 500, "className": "general-error", "data": {}, "errors": {} }
曾尝试向devices.class.js添加自定义find()方法,但这似乎无济于事。
我在这里搜索了类似的问题,并阅读了feathers-objection docs,但看不到我所缺少的内容。任何帮助将不胜感激。
我正在将羽毛js 4与异议ORM配合使用,并尝试创建一个返回异议js模型数据的服务,该数据包括一个渴望加载的BleongsToOneRelation,但我一直得到响应:...
app
变量之类的声音未定义。如果添加断点并检查调用堆栈以了解原因,调试器可能会为您提供很多帮助。也许您没有将其正确地传递到模块中。