如何在loopback中将查询参数添加到现有的远程方法中

时间:2016-03-22 04:16:24

标签: loopbackjs strongloop

我有两个型号user& order,用户可以拥有多个订单。所以我可以使用GET /users/{id}/orders来获取属于该用户的所有订单。

我想知道如何在HTTP请求中添加查询参数。例如GET /users/{id}/orders?terminated=true

数据库中没有名为terminated的字段,我只存储terminatedTime,所以我还需要进行计算。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您应该尝试where filter查询。如果您有一个名为terminated的布尔字段,则以下内容将起作用: /users/{id}/orders?filter={"where":{"terminated" : true}}

假设terminateTime是一个Date字段,则下面的URL应该在2016年3月16日之后找到具有terminateTime的用户的订单。

/users/{id}/orders?filter={"where":{"terminatedTime" : {"gt": "2016-03-16T00:00:00.000Z"}}}

注意:我曾使用StrongLoop API Explorer在我的示例应用程序上尝试类似的查询。根据您使用它的位置,可能需要也可能不需要双引号。

答案 1 :(得分:0)

如果您尝试访问模型中的查询字符串,这将有效。

var loopback = require('loopback');

module.exports = function(User) {
var findOne = User.findOne;

User.findOne = function(filter, cb) {
    var context = loopback.getCurrentContext(),
        terminated = context.active.http.req.query.terminated;

        //order processing logic