如何在loopback js中动态添加字段?

时间:2018-05-02 10:10:50

标签: loopbackjs strongloop

我的前端查询:

http://localhost:8888/api/employees?access_token=adsafsaf&filter= {%二条%22:{%22emp_code%22:%22EMPT01%22}}

原始查询:{"其中":{" emp_code":" EMPT01"}}

我正在尝试动态地在上面的传入请求中添加"代码" 字段。

Employee.observe('access', function (ctx, next) {
      // first way, it is not adding "orgId" field. 
      ctx.query.where = {
           orgId: ctx.options.data.orgId
      };
      // second way, it is not adding "orgId" field.
      const query = {};
      query['orgId'] = ctx.options.data.orgId;
      ctx.query.where = query;
      next();
  });

请有人指导我,哪里出错了?

Loopback版本:3

由于

1 个答案:

答案 0 :(得分:1)

我通过这种方式解决了这个问题。

Employee.observe('access', function (ctx, next) {
        // instead single , check both condition 
        if(ctx.query.where !=undefined){
            // if the request does not contain **where** then add it & filter it
            ctx.query.where.orgId = ctx.options.data.orgId;
        }else{
             ctx.query.where = {
                 orgId: ctx.options.data.orgId
             };
        }
        next();
  });