最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在打开路线时检查数据时出错

IT培训 admin 2浏览 0评论

在打开路线时检查数据时出错

我需要检查输入的数据和用户照片。

在这里,我保留字段进行验证

const validator = [
    check('name').exists().isLength({ min: 4, max: 20 }),
    check('email').exists().isEmail().normalizeEmail(),
    ...
];

我在这里擦拭一切

import { check, validationErrors } from 'express-validator/check';
import upload from'../config/multer.config.js';

router.post('/add', validator, (req, res) => {
    try {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            console.log(errors.array());
        }
        upload(req, res, (err) => {
            if (err) {
                return res.status(422).json({
                    err: err
                });
            }
            if (!req.files.avatar) {
                return res.status(422).json({
                    err: 'Missing required image file'
                });
            }
        })
    .....
    } catch (err) {
        console.log(err)
    }
});

我在检查!error.isEmpty()时收到错误消息,这是我的错误代码:

{ [ { location: 'body',
    param: 'name',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'name',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'email',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'email',
    value: undefined,
    msg: 'Invalid value' },
    .....
 } ]
  ....
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

我的问题是我没有验证用户输入的数据,错误将起作用

帮助处理我的问题,我需要检查输入的用户数据和照片,但是当用户输入超过所需数量的图像时我无法处理错误

我的multer.config.js:

import multer, { memoryStorage } from 'multer';
import path from 'path';

let storage = memoryStorage()
let upload = multer({
    storage: storage,
    limits: {
        fileSize: 1000000 
    },
    fileFilter: (req, file, cb) => {
        let ext = path.extname(file.originalname).toLowerCase();
        if (ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg') {
            return cb(null, false);
        }
        cb(null, true);
    }
}).fields([{
        name: 'avatar',
        maxCount: 1
    },
    {
        name: 'photos',
        maxCount: 3
    }
]);

export default upload;
回答如下:

我想通了,阅读文件后发现:Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.

我不得不改变const errors = validationResult(req);上的const errors = validationResult(req.body);

我的工作代码:

router.post('/signup', validatorSignup, (req, res) => {
    try {
        upload(req, res, (err) => {
            const errors = validationResult(req.body);
            if (!errors.isEmpty()) {
                return res.status(422).json({
                    err: errors.array()
                });
            }
            if (err) {
                return res.status(422).json({
                    err: err
                });
            }
            if (!req.files.user_avatar) {
                return res.status(422).json({
                    name: "MulterError",
                    message: 'Missing required image file',
                    field: "user_avatar"
                });
            } 
    ....   
    } catch (err) {
        console.log(err)
    }
});

在打开路线时检查数据时出错

我需要检查输入的数据和用户照片。

在这里,我保留字段进行验证

const validator = [
    check('name').exists().isLength({ min: 4, max: 20 }),
    check('email').exists().isEmail().normalizeEmail(),
    ...
];

我在这里擦拭一切

import { check, validationErrors } from 'express-validator/check';
import upload from'../config/multer.config.js';

router.post('/add', validator, (req, res) => {
    try {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            console.log(errors.array());
        }
        upload(req, res, (err) => {
            if (err) {
                return res.status(422).json({
                    err: err
                });
            }
            if (!req.files.avatar) {
                return res.status(422).json({
                    err: 'Missing required image file'
                });
            }
        })
    .....
    } catch (err) {
        console.log(err)
    }
});

我在检查!error.isEmpty()时收到错误消息,这是我的错误代码:

{ [ { location: 'body',
    param: 'name',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'name',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'email',
    value: undefined,
    msg: 'Invalid value' },
  { location: 'body',
    param: 'email',
    value: undefined,
    msg: 'Invalid value' },
    .....
 } ]
  ....
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

我的问题是我没有验证用户输入的数据,错误将起作用

帮助处理我的问题,我需要检查输入的用户数据和照片,但是当用户输入超过所需数量的图像时我无法处理错误

我的multer.config.js:

import multer, { memoryStorage } from 'multer';
import path from 'path';

let storage = memoryStorage()
let upload = multer({
    storage: storage,
    limits: {
        fileSize: 1000000 
    },
    fileFilter: (req, file, cb) => {
        let ext = path.extname(file.originalname).toLowerCase();
        if (ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg') {
            return cb(null, false);
        }
        cb(null, true);
    }
}).fields([{
        name: 'avatar',
        maxCount: 1
    },
    {
        name: 'photos',
        maxCount: 3
    }
]);

export default upload;
回答如下:

我想通了,阅读文件后发现:Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.

我不得不改变const errors = validationResult(req);上的const errors = validationResult(req.body);

我的工作代码:

router.post('/signup', validatorSignup, (req, res) => {
    try {
        upload(req, res, (err) => {
            const errors = validationResult(req.body);
            if (!errors.isEmpty()) {
                return res.status(422).json({
                    err: errors.array()
                });
            }
            if (err) {
                return res.status(422).json({
                    err: err
                });
            }
            if (!req.files.user_avatar) {
                return res.status(422).json({
                    name: "MulterError",
                    message: 'Missing required image file',
                    field: "user_avatar"
                });
            } 
    ....   
    } catch (err) {
        console.log(err)
    }
});

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论