我正在使用strongloop的loopbackjs来实现一个API。
对于模型Cat,我定义了一个远程方法,让我们称其为meow。
所以我可以这样做:
GET /cats/{:id}/meow
Cat模型belongsTo User模型。
现在我希望能够做这样的事情:
GET /users/{:id}/cats/{:id}/meow
有人知道怎么做吗?
我已经尝试过nestRemoting,它只适用于嵌套的“蓝图”方法。
发布于 2015-12-12 02:46:19
您可以在用户模型中定义一个远程方法,然后使用它来调用CatModel的meow方法
UserModel.someRemoteMethod = function(id1,id2,cb){
CatModel.meow(id2,cb);
}
UserModel.remoteMethod(
'someRemoteMethod',
{
accepts: [
{arg: 'id1', type: 'number', required: true},
{arg: 'id2', type: 'number', required: true}
],
http: {path: '/:id1/cats/:id2/meow', verb: 'get'}
}
);发布于 2016-05-18 13:20:42
使用nestRemoting('relationName')。它没有很好的文档记录,但对于您的模型,您可以使用:
User.on('attached', function() {
User.nestRemoting('catRelation');
}把它放到你的user.js文件中,你就应该得到你想要的端点了。
发布于 2017-12-10 04:38:16
我有一个解决方案需要和大家分享
nestRemoting函数接受一个options json对象,其中包含一个名为filterMethod的属性。此方法过滤模型函数以仅获取默认方法,因此我在( else if )中传递了此带有自定义的属性回调函数,以检查我的远程方法( DoWhat )并返回它
server.models.Client.nestRemoting('units', {filterMethod: function(method, relation) {
let regExp = /^__([^_]+)__([^_]+)$/;
let matches = method.name.match(regExp);
if (matches) {
return '__' + matches[1] + '__' + relation.name + '__' + matches[2];
} else if (method.name === 'DoWhat') {
return method.name;
}
}});我希望这个解决方案能帮助你
https://stackoverflow.com/questions/34162099
复制相似问题