Как динамически добавить поле в loopback js?

Мой внешний запрос:

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

исходный запрос: {"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

Спасибо


person uday214125    schedule 02.05.2018    source источник
comment
Откуда ctx.options.data в запросе на получение? Я проверил ваш код без него, и он работает. Также вы не добавляете к запросу, вы заменяете его.   -  person    schedule 02.05.2018
comment
ctx.options.data : исходит из промежуточного программного обеспечения.   -  person uday214125    schedule 02.05.2018
comment
Попробуйте опубликовать строку отладки соединителя. loopback.io/doc/en/lb3/Setting-debug-strings. html   -  person    schedule 02.05.2018


Ответы (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();
  });
person uday214125    schedule 03.05.2018