2016-03-07 43 views
2

我的API端点就像是一个模型定义如下product.js如何在回送模型中访问查询字符串参数?

API /产品/ 9720?id_shop = 1 & id_lang = 1

我需要访问id_shop在product.js到应用它从产品表中提取记录之前的where子句。

Product.observe('access', function (ctx, next) { 
    next(); 
}); 

我该如何访问id_shop和id_lang?

+0

谢谢你,你救了我的一天! – viam

回答

4

您可以使用远程方法来创建自定义端点:

https://docs.strongloop.com/display/public/LB/Remote+methods

如果你真的想改变Model.find(),您可以使用loopback.getCurrentContext(默认行为)和再注入过滤器为每个GET请求:

Product.on('dataSourceAttached', function(obj){ 
    var find = Product.find; 
    Product.find = function(filter, cb) {   
     var id_shop = loopback.getCurrentContext().active.http.req.query.id_shop;   
     filter = {where:{id_shop: id_shop}}; 
     return find.apply(this, arguments); 
    }; 
}); 

这将覆盖传入任何过滤器,所以你需要处理与额外的逻辑。

相关问题