ArangoDB中的主体参数

fcwjkofz  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(161)

我在Foxx Microservices中编写了以下路由器:

router.post('/PersonInformation', function (req, res) {
    const keys = db._query(aql`
    FOR p IN Personal
                FILTER (p.stateId IN ${req.queryParams.statePar} OR IS_NULL(${req.queryParams.statePar}))
                AND p.empDate >= TO_NUMBER(${req.queryParams.fromDatePar}) AND p.empDate <= TO_NUMBER(${req.queryParams.toDatePar})
                          RETURN p
  `);
  res.send(keys);
  })
  .queryParam('statePar', joi.array().default(null), 'State Parameter')
  .queryParam ('fromDatePar', joi.string().required(), 'FromDate Parameter')
  .queryParam('toDatePar', joi.string().required(), 'ToDate Parameter')
  .response(joi.array().items(
  joi.string().required()
  ).required(), 'List of entry keys.')
  .summary('List entry keys')
  .description('Assembles a list of keys of entries in the collection.');

我如何将queryParam转换为body参数。我使用.body代替.queryParam,但它没有响应。我还编写了如下表,但它仍然不起作用:

router.post('/PersonInformation', function (req, res) {
  const distributorIdPar = req.body;
  const cityPar = req.body;
  const productIdPar = req.body;
  const fromDatePar = req.body;
  const toDatePar = req.body;
  const keys = db._query(aql`
    const keys = db._query(aql`
    FOR p IN Personal
                FILTER (p.stateId IN ${req.body.statePar} OR IS_NULL(${req.body.statePar}))
                AND p.empDate >= TO_NUMBER(${req.body.fromDatePar}) AND p.empDate <= TO_NUMBER(${req.body.toDatePar})
                          RETURN p
  `);
  res.send(keys);
  })
  .response(joi.array().items(
  joi.string().required()
  ).required(), 'List of entry keys.')
  .summary('List entry keys')
  .description('Assembles a list of keys of entries in the collection.');
mwyxok5s

mwyxok5s1#

您有一个重复的const keys = db._query(aql``行,它破坏了语法。此外,您没有设置.body(joi.object()).body(['application/json']),这导致req.body成为一个缓冲区: 如果尚未为当前路径定义几何体,则该值将与rawBody -[docs](https://www.arangodb.com/docs/stable/foxx-reference-routers-request.html)相同 您还将req.body赋给各种变量,但实际上并不使用它们。 我将使用解构方法将body参数赋给局部变量以获得保证值,并对statePar使用常规赋值,以便在body中省略它时可以默认为null。此外,我将对fromDatePartoDatePar强制使用数值(注意,这需要joi中的strict(),否则它将很乐意接受数字字符串)并从查询中删除TO_NUMBER()。下面的代码接受statePar的字符串数组,或者期望该属性不存在。它不允许使用"statePar":null"statePar":[]"statePar":[null]`,但您可以根据需要进行更改。

'use strict';
const joi = require('joi');
const { db, aql } = require('@arangodb');
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();

router.post('/PersonInformation', function (req, res) {
  const statePar = req.body.statePar || null;
  const { fromDatePar, toDatePar } = req.body;
  const keys = db._query(aql`
    FOR p IN Personal
      FILTER (p.stateId IN ${statePar} OR IS_NULL(${statePar}))
      AND p.empDate >= ${fromDatePar} AND p.empDate <= ${toDatePar}
      RETURN p
  `);
  res.send(keys);
})
.body(joi.object({
  statePar: joi.array().items(
    joi.string().required()
  ),
  fromDatePar: joi.number().required(),
  toDatePar: joi.number().required(),
}).required().strict(), 'Search parameters')
.response(joi.array().items(
  joi.string().required()
).required(), 'List of entry keys.')
.summary('List entry keys')
.description('Assembles a list of keys of entries in the collection.');

module.context.use(router);

相关问题