2017-11-18 51 views
1

我的代碼片段顯示了我定義的路由器,檢查主體參數並檢查驗證錯誤。如何在使用Nodejs的Express-validator中驗證multipart/form-data

我的定義POST請求:

router.post("/addEmployee",upload.any(), function(req, res, next) { 

    /*I used multer because body data from multipart/form-data*/ 

var input = JSON.parse(req.body.data); 

服務器驗證: //驗證不起作用,因爲req.checkBody只得到bodyData我現在用的multipart/form-data的(req.body.data )

req.checkBody('EMPID', "**EMPID must be Integer**").notEmpty().isNumeric(); 

req.checkBody('PAYROLLID', "**PAYROLLID must be Integer**").notEmpty().isNumeric(); 


..... 

.... 

檢查驗證錯誤

var validationError = req.validationErrors(); //check error 


// if (validationError) { //suppose get error -- throw that error 


res.json(validationError[0].msg); 


} else { //validation success 


......strong text 
+0

請閱讀如何[創建一個最小的,完整的,並且可驗證的示例](https://stackoverflow.com/help/mcve) –

回答

0

傢伙,我找到一個解決方案

router.post("/addEmployee",upload.any(), function(req, res, next) { 

    /*I used multer because body data from multipart/form-data*/ 

    var input = JSON.parse(req.body.data); 

    req.body = input;// solution this line 
}); 
相關問題