我知道如何创建这样的自定义远程方法:
module.exports = function (Dog) {
Dog.myMethod = function(age, owner, cb);
Dog.find({where: {age: age, owner: owner}}, function(err, result) {
var myResult = new Array();
// transform "result" to "myResult"
cb(null, myResult);
});
Dog.remoteMethod('myMethod', {
http: {path: '/list', verb: 'get'},
accepts: [
{arg: 'age', type: 'number', http: {source: 'query'}},
{arg: 'owner', type: 'string', http: {source: 'query'}}
],
returns: {arg: 'list', type: 'json'}
});
});
使用以下网址:
本地主机:3000 / API /狗/ myMethod的年龄= 3及所有者=乔
但我需要这样称呼:
本地主机:3000 / API /狗年龄= 3及所有者=乔
我在Strongloop API文档中发现了这篇文章(在第34节中;更改内置方法的实现",小节"通过您的模型' s脚本"):{{ 3}}
但它没有显示如何处理自定义参数。
https://docs.strongloop.com/display/public/LB/Customizing+models没有给我答案。
感谢您的帮助!
答案 0 :(得分:0)
好的,我找到了方法。
我所要做的就是添加这一行:
Dog.disableRemoteMethod('find', true); // Removes (GET) /dog
在此之前(我还必须将“/ list”更改为“/”以使其正常工作):
Dog.remoteMethod('myMethod', {
http: {path: '/', verb: 'get'},
...
)};
所以现在我可以像这样向URL发出请求: localhost:3000 / api / dogs?age = 3& owner = joe
整个代码是:
module.exports = function (Dog) {
Dog.myMethod = function(age, owner, cb);
Dog.find({where: {age: age, owner: owner}}, function(err, result) {
var myResult = new Array();
// transform "result" to "myResult"
cb(null, myResult);
});
Dog.disableRemoteMethod('find', true); // Removes (GET) /dog
Dog.remoteMethod('myMethod', {
http: {path: '/', verb: 'get'},
accepts: [
{arg: 'age', type: 'number', http: {source: 'query'}},
{arg: 'owner', type: 'string', http: {source: 'query'}}
],
returns: {arg: 'list', type: 'json'}
});
});