gpt4 book ai didi

loopbackjs - 环回有很多关系远程方法钩子(Hook)

转载 作者:行者123 更新时间:2023-12-03 16:44:26 31 4
gpt4 key购买 nike

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

person.json:

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

friend .json

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

},
"acls": [],
"methods": []
}

我想在调用 POST/api/Persons/{id}/friends 时使用 beforeRemote。

所以我在 person.js 中编码

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

但它不起作用!

一开始我认为是'__create__friends'的问题,但是当我person.js 中的代码如下:

    module.exports = function(Person) {
Person.disableRemoteMethod('__create__friends');
};

我可以成功禁用“__create__friends”。

那么问题出在哪里?

谁能帮帮我?

最佳答案

由于相关模型的方法附加到 Person 原型(prototype),所以你应该像这样注册钩子(Hook):

Person.beforeRemote('prototype.__create__friends', function() {
next()
})

关于loopbackjs - 环回有很多关系远程方法钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327042/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com