【问题标题】:How to add field dynamically in loopback js?如何在环回js中动态添加字段?
【发布时间】:2018-05-03 18:13:53
【问题描述】:

我的前端查询:

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

原始 where 查询:{"where":{"emp_code":"EMPT01"}}

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

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();
  });

请高人指点一下,哪里错了?

环回版本:3

谢谢

【问题讨论】:

  • ctx.options.data 在获取请求中来自哪里?我已经在没有它的情况下测试了你的代码并且它正在工作。此外,您没有添加到查询中,而是替换它。
  • ctx.options.data : 它来自中间件。
  • 尝试发布您的连接器调试字符串。 loopback.io/doc/en/lb3/Setting-debug-strings.html

标签: loopbackjs strongloop


【解决方案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();
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 2021-03-23
    • 2017-09-09
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多