【问题标题】:Eager loading with Feathers JS 4 and Objection JS - Cannot read property 'get' of undefined使用 Feathers JS 4 和 Objection JS 急切加载 - 无法读取未定义的属性“get”
【发布时间】:2020-05-14 12:29:43
【问题描述】:

我正在使用带有异议 ORM 的羽毛 js 4 并尝试创建一个服务,该服务返回包含急切加载的 BleongsToOneRelation 的异议 js 模型数据,但我不断收到响应:

无法读取未定义的属性“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,但就是看不到我缺少什么。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript feathersjs objection.js


    【解决方案1】:

    听起来您的 app 变量未定义。如果您添加断点并检查调用堆栈以了解原因,调试器可以为您提供很多帮助......也许您没有将它正确地传递给您的模块。

    这是引发错误的代码:

    https://runkit.com/embed/kipjj807i90l

    【讨论】:

    • 感谢您的帮助。我尝试在服务顶部运行调试器,并且应用程序似乎总是被定义,在模型顶部也是如此。对你提供的 runkit 代码有点不确定,假设第 5 行应该是:a.get('knex') 你知道这段代码的实际位置,以便我可以逆向工作并找到问题吗?
    • 在 runkit 代码中,这只是一个示例,表明如果您尝试调用 <undefined var>.get(...) 函数,抛出的错误与您从 API 得到的错误完全相同
    【解决方案2】:

    发生这种情况是因为在您的 relationMappings 中,您正在初始化模型而没有预期的 app 参数。在feathers-objection examples 中展示了如何修改模型以便在关系映射中使用它们:您必须显式处理缺少的app 参数。

    【讨论】:

      【解决方案3】:

      我知道这已经很晚了,但我刚刚发现了同样的问题。

      解决方案是根据模型部分here,使用以下内容修改羽毛反对模型

      module.exports = function(app) {
        if (app) {
          const db = app.get('knex');
          .... etc
        }
      
        return User;
      };
      
      module.exports = User;
      

      【讨论】:

        猜你喜欢
        • 2021-04-29
        • 1970-01-01
        • 2013-07-18
        • 2018-03-15
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 2021-11-02
        相关资源
        最近更新 更多