loopback hasMany relation remoteMethod hook

王如锵*_*王如锵 3 loopbackjs

我正在使用loopback开发自己的网站.但最近我遇到了hasMany remoteMethod的问题.这是问题:我有两个模型:

person.json:

{
  "name": "Person",
  "base": "PersistedModel",
  "strict": true,
  "idInjection": true,
  "properties": {
    /*...
    ....
    */
  },
  "validations": [],
  "relations": {
    "friends": {
      "type": "hasMany",
      "model": "Friend",
      "foreignKey": "personId"
    }
  },
  "acls": [],
  "methods": []
}
Run Code Online (Sandbox Code Playgroud)

friend.json

friend.json:
{
  "name": "friend",
  "base": "PersistedModel",
  "strict": true,
  "idInjection": true,
  "properties": {
    /*...
    ....
    */
  },
  "validations": [],
  "relations": {

  },
  "acls": [],
  "methods": []
}
Run Code Online (Sandbox Code Playgroud)

当我调用POST/api/Persons/{id}/friends时,我想使用beforeRemote.

所以我在person.js中编码

    module.exports = function(Person) {
        Person.beforeRemote('__create__friends', function(ctx, instance, next) {
            /*
                code here
            */
        });
    };
Run Code Online (Sandbox Code Playgroud)

但它不起作用!

一开始我认为这是'__create__friends'的问题,但是当我在person.js中编码时:

    module.exports = function(Person) {
        Person.disableRemoteMethod('__create__friends');
    };
Run Code Online (Sandbox Code Playgroud)

我可以成功禁用'__create__friends'.

所以有什么问题?

谁能帮我?

Iva*_*nZh 8

因为相关模型的方法附加到Person原型,所以应该像这样注册钩子:

Person.beforeRemote('prototype.__create__friends', function() {
    next()
})
Run Code Online (Sandbox Code Playgroud)