0

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

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

1 回答 1

0

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

如果没有为当前路由定义 body,则该值将与 rawBody 相同 – docs

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

我会使用解构为保证值将主体参数分配给局部变量,并使用常规分配,statePar以便它可以默认为null如果它从主体中省略。fromDatePar此外,我将对and强制执行数值toDatePar(请注意,这需要strict()在 joi 中,否则它将很乐意接受数字字符串)并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);
于 2021-09-01T10:55:30.947 回答