无法随上传检索正文,Express js

Cannot retrieve body text along with upload, Express js

我有一个表格,要求提供文本和文件。我使用 multer 进行文件上传。问题是如果我使用 enctype=multipart/form-data

,我将无法使用 req.body 检索文本

路由文件

router.post('/new-job', function(req,res,next){
  upload(req,res,function(err) {
   if(err) {
      return res.end("Error uploading file.");
   }
  });

  var newJob = {
    job_name: req.body.job_name, //Cannot retrieve this two
    job_desc: req.body.job_desc, 
  };

  var newJobData = new Jobs(newJob);
  newJobData.save(function(err,user){
  if(err)
    console.log(err);
  });

  res.render('jobs/new-job', {job_added:true});
}); 

多重配置

var multer = require('multer');
var  storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, 'public/uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.originalname);
  }
});

备注

在解析请求之前,您无法访问 req.body 内容,因此要么将代码移到 upload() 回调中, 要么 摆脱显式 upload() 完全调用,只需将 upload 放在路由处理程序之前:

router.post('/new-job', upload, function(req, res, next) {
  var newJob = {
  // ...