我们在REST API中使用LoopBack,并且需要从LoopBack模型中的自定义逻辑中访问查询过滤器(在客户端中指定)。例如,给定此查询:
http://localhost:1337/api/Menus/formatted?filter[where][id]=42
我们如何访问'其中'来自' Menu.formatted'中的参数代码:
function asMenu(Menu) {
Menu.formatted = function (callback) {
<<Need to access the query filter here...>>
答案 0 :(得分:4)
引入过滤器的方法应该与此类似:
module.exports = function(Menu) {
Menu.formatted = function (filter,callback) {
// Your code here
}
Menu.remoteMethod('formatted', {
http: { path: '/formatted', verb: 'get' },
accepts: [
{ arg: 'filter', type: 'object', 'http': { source: 'query' } }
],
returns: { type: 'object', root: true }
});
};
在上面的示例中,在accepts
字段中,它表示远程方法接收的参数,您需要添加filter
参数。这样,您可以将filter
的查询参数值用作对象。
答案 1 :(得分:2)
将filter
查询参数声明为formatted
远程方法的参数,然后像callback
参数一样访问它。