Loopback.js-如何传递多个参数来建模远程方法

时间:2019-07-16 15:42:05

标签: node.js api loopbackjs

我正在尝试为仅具有一个参数(id)的Idioma模型更改一种远程方法。这是实际的功能代码:

Model.remoteMethod('idiomaByOferta', {
  description: 'Obtener un idioma por oferta',
  http: { path: '/oferta/:id', verb: 'get' },
  accepts: [
    { arg: 'id', type: 'string', required: true },
    { arg: 'res', type: 'object', http: { source: 'res' } }
  ],
  returns: {
    type: 'Object',
    root: true,
    default: output_structure
  }
});

Model.idiomaByOferta = (id, res, cb) => {
  parameterValidatorId(id, err => {
    if (err) {
      res.status(httpStatus.BAD_REQUEST.code).send(err);
    }
  });
  const conn = Model.app.datasources.db.connector;
  commons
    .getResultSqlString(conn, sqlEstablecimiento.findIdiomas, [id])
    .then(stb => {
      cb(null, stb);
    })
    .catch(err => cb(err, null));
  };

Model.afterRemote('idiomaByOferta', async (ctx, result, next) => {
  delete ctx.res.req.query.limit;
  delete ctx.res.req.query.page;
  delete query.limit;
  delete query.page;
  next();
});

现在,我想包含另一个参数,但是我还没有确切找到如何使用必需的参数来做到这一点。我已经尝试了以下方法,但是不起作用:

Model.remoteMethod('idiomaByOferta', {
  description: 'Obtener un idioma por oferta',
  http: { path: '/oferta', verb: 'get' },
  accepts: [
    { arg: 'id', type: 'string', required: true, http: { source: 'query' }},
    { arg: 'nivel', type: 'string', required: true, http: { source: 'query' }},
    { arg: 'res', type: 'object', http: { source: 'res' } }
  ],
  returns: {
    type: 'Object',
    root: true,
    default: output_structure
  }
});

请求网址:{{url}} / api / idiomas / oferta?id = {{{oferta}}&nivel = Inicial

响应:

{
    "errors": [
        {
            "code": 938,
            "source": "id",
            "detail": "is not allowed"
        },
        {
            "code": 963,
            "source": "nivel",
            "detail": "is not allowed"
        }
    ]
}

我也尝试过这样做:

Model.remoteMethod('idiomaByOferta', {
  description: 'Obtener un idioma por oferta',
  http: { path: '/oferta/:id/nivel/:nivel', verb: 'get' },
  accepts: [
    { arg: 'id', type: 'string', required: true},
    { arg: 'nivel', type: 'string', required: true},
    { arg: 'res', type: 'object', http: { source: 'res' } }
  ],
  returns: {
    type: 'Object',
    root: true,
    default: output_structure
  }
});

请求超时,并且永远不会完成。

1 个答案:

答案 0 :(得分:0)

accepts属性的位置很重要。

http属性中,参数path是可选的,如果要更改accepts属性的顺序或仅修改路径名,该参数很有用。

我要做的是:

Model.remoteMethod('idiomaByOferta', {
  description: 'Obtener un idioma por oferta',
  http: { path: '/oferta/:id/:nivel', verb: 'get' },
  accepts: [
    { arg: 'id', type: 'string', required: true },
    { arg: 'nivel', type: 'string', required: true},
    { arg: 'res', type: 'object', http: { source: 'res' } }
  ],
  returns: {
    type: 'Object',
    root: true,
    default: output_structure
  }
});

Model.idiomaByOferta = (id, nivel, res, cb) => { //add nivel here in second position
  parameterValidatorId(id, err => {
    if (err) {
      res.status(httpStatus.BAD_REQUEST.code).send(err);
    }
  });
  parameterValidatorId(nivel, err => {
    if (err) {
      res.status(httpStatus.BAD_REQUEST.code).send(err);
    }
  });
  const conn = Model.app.datasources.db.connector;
  commons
    .getResultSqlString(conn, sqlEstablecimiento.findIdiomas, [id, nivel]) //and use it there, maybe, depending on what your code is doing?
    .then(stb => {
      cb(null, stb);
    })
    .catch(err => cb(err, null));
  };