Express 验证器返回值:未定义消息:'Invalid value'
Express validator returning value : undefined msg: 'Invalid value'
路线
const express = require('express');
const { check } = require('express-validator');
const logControllers = require('../controllers/log-controllers');
const router = express.Router();
router.post('/',
[
check('title')
.not()
.isEmpty()
],
logControllers.createLogEntry
);
日志控制器
const uuid = require('uuid/v4');
const { validationResult } = require('express-validator');
const HttpError = require('../models/http-error');
let DUMMY_LOGENTRIES = [
{
id: "j1",
title: "king"}]
const createLogEntry = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors);
throw new HttpError('Invalid inputs passed, please check your data.', 422);
}
const { title } = req.body;
const createdLogEntry =
//new logEntry(
{
id: uuid(), title
};
DUMMY_LOGENTRIES.push(createdLogEntry);
res.status(201).json({ logEntry: createdLogEntry });
};
当我删除验证器时,POST 请求有效,但使用验证器 Nodemon returns:
formatter: [Function: formatter],
errors: [
{
value: undefined,
msg: 'Invalid value',
param: 'title',
location: 'body'
},
奖金问题: 没有验证器,returned 响应只是 uuid...不是标题,我不知道为什么,因为我问对于完整的 createdLogEntry,它应该 return 标题和 uuid。想法?
没有找到验证器方面缺少的内容,但我确实发现我在 Postman 中返回的是文本而不是 JSON。只是重写了所有内容并且它起作用了。猜猜是语法。
路线
const express = require('express');
const { check } = require('express-validator');
const logControllers = require('../controllers/log-controllers');
const router = express.Router();
router.post('/',
[
check('title')
.not()
.isEmpty()
],
logControllers.createLogEntry
);
日志控制器
const uuid = require('uuid/v4');
const { validationResult } = require('express-validator');
const HttpError = require('../models/http-error');
let DUMMY_LOGENTRIES = [
{
id: "j1",
title: "king"}]
const createLogEntry = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors);
throw new HttpError('Invalid inputs passed, please check your data.', 422);
}
const { title } = req.body;
const createdLogEntry =
//new logEntry(
{
id: uuid(), title
};
DUMMY_LOGENTRIES.push(createdLogEntry);
res.status(201).json({ logEntry: createdLogEntry });
};
当我删除验证器时,POST 请求有效,但使用验证器 Nodemon returns:
formatter: [Function: formatter],
errors: [
{
value: undefined,
msg: 'Invalid value',
param: 'title',
location: 'body'
},
奖金问题: 没有验证器,returned 响应只是 uuid...不是标题,我不知道为什么,因为我问对于完整的 createdLogEntry,它应该 return 标题和 uuid。想法?
没有找到验证器方面缺少的内容,但我确实发现我在 Postman 中返回的是文本而不是 JSON。只是重写了所有内容并且它起作用了。猜猜是语法。