ArangoDB 中的正文参数

Body parameter in ArangoDB

我在 Foxx 微服务中编写了以下路由器:

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,但它没有回答。我也写了table如下,还是不行:

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.');

您有一个重复的行 const keys = db._query(aql` 破坏了语法。此外,您没有设置 .body(joi.object()).body(['application/json']),这导致 req.body 成为缓冲区:

If no body has been defined for the current route, the value will be identical to rawBody – docs

您也将 req.body 分配给各种变量,但实际上并未使用它们。

我会使用保证值的解构将主体参数分配给局部变量,并为 statePar 使用常规赋值,这样如果它从主体中省略,它可以默认为 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);