loopback hasMany 关系 remoteMethod 挂钩
loopback hasMany relation remoteMethod hook
我正在使用环回开发我自己的网站。
但是最近我遇到了 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 原型,你应该像这样注册钩子:
Person.beforeRemote('prototype.__create__friends', function() {
next()
})
我正在使用环回开发我自己的网站。 但是最近我遇到了 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 原型,你应该像这样注册钩子:
Person.beforeRemote('prototype.__create__friends', function() {
next()
})