Loopbackjs 如何在环回模型中访问查询字符串参数?

Loopbackjs 如何在环回模型中访问查询字符串参数?,loopbackjs,Loopbackjs,对于模型定义product.js,我的api端点如下所示 api/products/9720?id_shop=1和id_lang=1 我需要访问product.js中的id_shop,以便在它从products表中获取记录之前应用where子句 Product.observe('access', function (ctx, next) { next(); }); 如何访问id_shop和id_lang?您可以使用远程方法创建自定义端点: 如果确实要更改Model.find()的默认

对于模型定义product.js,我的api端点如下所示

api/products/9720?id_shop=1和id_lang=1

我需要访问product.js中的id_shop,以便在它从products表中获取记录之前应用where子句

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

如何访问id_shop和id_lang?

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

如果确实要更改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);
    };
});

这将覆盖传入的任何筛选器,因此您需要使用附加逻辑来处理它。

谢谢,伙计,您救了我一天!