我正在尝试使用 loopback-connector-remote 数据源连接 2 个环回服务,例如 A 和 B。
在B上,我有这个自定义远程方法:
/api/B/myModel/myMethod/{id}
如果我访问 B 服务上的 API 浏览器,此方法可以正常工作。
然后在 A 服务上我想访问此方法,因此我在远程模型对象上创建了以下配置(在 B 上也相同):
myModel.remoteMethod(
'myMethod',
{
http: {path: '/myMethod/:id', verb: 'get'},
accepts: [
{arg: 'id', type: 'number', required: true}
],
returns: {type: 'object', root: true}
}
);
从A,我可以对B进行任何调用,例如find、findById等。但是当我调用此自定义方法时,我在A上收到此错误:
strong-remoting:rest-adapter Error in GET /myModel/myMethod/1231: Error: id must be a number
查看B中的日志,我看到A正在像这样调用服务:
strong-remoting:rest-adapter Error in GET /myModel/myMethod/:id?id=1231: Error: id must be a number
为什么在创建 URL 时,strong-remoting 或 loopback-connector-remote 没有正确替换 ID?我是否遗漏了一些配置?
最佳答案
需要将源路径添加到A服务模型对象中的远程方法配置:
{ arg: 'id', type: 'number', required: true, http: { source: 'path' }}
关于javascript - 使用loopback-connector-remote调用另一个环回服务中的自定义方法不会创建正确的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36853294/