我的多部分(表单数据)上的POST请求有问题。其他主题中的所有想法都不起作用。
console.log(req.body);
时,我获得undefined
这是我的代码:
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cors=require('cors');
//var index = require('./routes/index');
//var users = require('./routes/users');
var UsersDB=require('./routes/UsersDB');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(cors());
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
//app.use('/', index);
//app.use('/users', users);
app.use('/Users',UsersDB);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

UsersDB.js
var express = require('express');
var router = express.Router();
var Users=require('../models/Users');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + ".png")
}
});
var upload = multer({ storage: storage }).single('avatar');
//** POST ** ROUTE : http://localhost:3000/Users/
router.post('/',function(req,res,next)
{
console.log(req.body.firstName);
/*
Users.addUsers(req.body,function(err,count)
{
if(err)
{
res.json(err);
}
else
{
res.json(req.body);//or return count for 1 & 0
}
});
*/
upload(req, res, function (err) {
if (err) {
// An error occurred when uploading
return
}
// Everything went fine
})
});
module.exports=router;

邮差错误:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1>Cannot read property 'firstName' of undefined</h1>
<h2></h2>
<pre>TypeError: Cannot read property 'firstName' of undefined
at /mnt/c/Users/Elodie/Desktop/server/routes/UsersDB.js:69:25
at Layer.handle [as handle_request] (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/layer.js:95:5)
at next (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/layer.js:95:5)
at /mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/index.js:335:12)
at next (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/index.js:174:3)
at router (/mnt/c/Users/Elodie/Desktop/server/node_modules/express/lib/router/index.js:47:12)</pre>
</body>
</html>
&#13;
答案 0 :(得分:1)
由于您正在使用multer来解析请求正文,因此您需要在访问req.body
之前调用multer。您当前使用multer的方式,它将从请求中提取信息以处理文件,但不会成为中间件堆栈的一部分,因此在调用路由之前,您无法获得正文解析的好处。
将multer中间件添加到路由签名中,因此在调用路由回调之前,multer将解析请求主体。例如像这样
router.post('/', upload.single('avatar'), (req, res, next) => {
console.log(req.body.firstName)
})