0

我有一个模型mod,它具有belongsTo relation另一个模型notey

型号mod

 {
  "name": "mod",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "mod1": {
      "type": "number",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "noteyRelation": {
      "type": "belongsTo",
      "model": "notey",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    }
  },
  "acls": [],
  "methods": {}
}

型号notey

{
  "name": "notey",
  "plural": "noteys",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

我知道环回会自动创建一个函数Mod.__get__noteys()来获取任何给定notey的 amod所属的mod id

/mods/{id}/noteyRelation

但是我TypeError: Cannot read property 'apply' of undefined在进行 API 调用时遇到错误/mods/{id}/noteyRelation,我认为这与覆盖方法有关Mod.findOne()。调用时,覆盖的方法按预期工作/mods/{id}

我的定义Mod.findOne()

Mod.once("attached", function () {
    Mod.findOne = function (id, filter, callback) {
      console.log("Inside Override ", id, " and ", filter);
      let err = "";
      console.log("Callback is: ", callback.toString());

      console.log("sending response");
      callback(null, {
        mod1: "2",
        id: "2",
        noteRelationId: "0",
        noteyRelationId: "1",
      });
    };
  });

我相信内部Mod.__get__noteys()调用Mod.findOne(),因为当我评论我的实现时Mod.findOne(),API 调用/mods/{id}/noteyRelation会产生预期的结果。

我还想Mod.__get__noteys()在某个时候覆盖并发现文档将此方法提到为Mod.protoype.__get__noteys(). 我还没有弄清楚其中的区别。

错误转储:

Inside Override  { where: { id: 2 } }  and  {
  prohibitHiddenPropertiesInQuery: true,
  maxDepthOfQuery: 12,
  maxDepthOfData: 32,
  accessToken: {
    id: 'DQp6XSdkzD176w7NLrlBY8FPdy2YSWqQG0Q4yqRebeNlzwbO59wAGWyUHejIgYqw',
    ttl: 1209600,
    created: 2021-08-03T10:15:25.718Z,
    userId: 1
  }
}
Callback is:  function(err, model) {
          if (err) {
            fn(err);
          } else if (model) {
            fn(null, model);
          } else {
            err = new Error(g.f('could not find a model with {{id}} %s', id));
            err.statusCode = 404;
            err.code = 'MODEL_NOT_FOUND';
            fn(err);
          }
        }
sending response
Unhandled error for request GET /mods/2/noteyRelation?access_token=DQp6XSdkzD176w7NLrlBY8FPdy2YSWqQG0Q4yqRebeNlzwbO59wAGWyUHejIgYqw: TypeError: Cannot read property 'apply' of undefined
    at Object.fn (D:\notes\node_modules\loopback-datasource-juggler\lib\relation-definition.js:1381:7)
    at SharedMethod.invoke (D:\notes\node_modules\strong-remoting\lib\shared-method.js:270:27)
    at HttpContext.invoke (D:\notes\node_modules\strong-remoting\lib\http-context.js:300:12)
    at phaseInvoke (D:\notes\node_modules\strong-remoting\lib\remote-objects.js:695:9)
    at runHandler (D:\notes\node_modules\loopback-phase\lib\phase.js:135:5)
    at D:\notes\node_modules\async\dist\async.js:3110:16
    at replenish (D:\notes\node_modules\async\dist\async.js:1011:17)
    at D:\notes\node_modules\async\dist\async.js:1016:9
    at eachLimit$1 (D:\notes\node_modules\async\dist\async.js:3196:24)
    at Object.<anonymous> (D:\notes\node_modules\async\dist\async.js:1046:16)
    at runHandlers (D:\notes\node_modules\loopback-phase\lib\phase.js:144:13)
    at D:\notes\node_modules\async\dist\async.js:3110:16
    at replenish (D:\notes\node_modules\async\dist\async.js:1011:17)
    at D:\notes\node_modules\async\dist\async.js:1016:9
    at eachLimit$1 (D:\notes\node_modules\async\dist\async.js:3196:24)
    at Object.<anonymous> (D:\notes\node_modules\async\dist\async.js:1046:16)
4

0 回答 0