我正在嘗試將 express-validator 與 multer 結合使用來驗證還包含影像、標題和引數的 POST 請求的主體組件。我嘗試使用 body() 樣式驗證方法和 checkSchema 樣式,但都不能正確驗證我的正文文本欄位。checkSchema 樣式將適用于我的標題和引數,但由于某種原因不適用于正文。也許我需要重新安排一些事情?
const express = require('express');
const multer = require('multer');
const { checkSchema } = require('express-validator');
const router = express.Router();
const upload = multer();
// inputs coming in from everywhere - header, params and form-data
router.post(
'/:aId/help/:tId/blah',
checkSchema({
aId: {
// The location of the field, can be one or more of body, cookies, headers, params or query.
// this check works!
in: ['params'],
errorMessage: 'aId is wrong',
isAscii: true
},
tId: {
// this check works
in: ['headers'],
errorMessage: 'pId is wrong',
isAscii: true
},
uploadedTime: {
// these checks fail to work
in: ['body'],
errorMessage: 'uploadedTime is wrong',
isInt: true
},
uploadedByName: {
// these checks fail to work
in: ['body'],
errorMessage: 'uploadedByName is wrong',
isInt: false
}
}),
upload.single('file'),
async (req, res) => {
console.log('ANY REQ params?', req.params);
console.log('ANY headers?', req.headers);
console.log('ANY BODY?', req.body);
console.log('ANY FILES?', req.file);
}
);

我的日志顯示了正文:
ANY BODY? [Object: null prototype] {
uploadedTime: '1646762107739',
uploadedByName: 'Moe S'
}
uj5u.com熱心網友回復:
multer 使用需要在驗證之前進行
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/448863.html
下一篇:無法使用和連接到MongoDB
