Express, Multer, BodyParser req.body empty array

六月ゝ 毕业季﹏ 提交于 2019-12-25 07:38:54

问题


Sorry for such a noob question, but I have a form upload images and add some text to the DB, the images are uploading fine, but the req.body object is always an empty array.

HTML

<form class='new-project' action='/projects' method='POST' enctype="multipart/form-data">
  <input type="text" name="title" placeholder="Project Title">
  <br>
  <textarea name="description" rows="8" cols="40" placeholder="Project description"></textarea>
  <br>
  <label for='file'>Select your image:</label>
  <input type='file' accept='image/*' name='uploadedImages' multiple/>
  <span class='hint'>Supported files: jpg, jpeg, png.</span>
  <br>
  <input type="submit" value="uploading_img">
</form>

JS

var bodyParser = require('body-parser'),
express    = require('express'),
multer     = require('multer'),

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

var upload = multer({storage: storage}).any('uploadedImages');

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.post('/projects', function(req, res){
  console.log('req.body');
  console.log(req.body);
  upload(req, res, function(err){
    if(err){
      console.log('Oh dear...');
      console.log(err);
      return;
    }
    console.log(req.files);
    res.end('Your files uploaded!');
    console.log('Yep yep!');
  });
});

According to the documentation, multer add a body object to the request, but I only get an empty array back with or without body-parser.

Thanks for taking the time to look through the code!!


回答1:


Your can get desired req.body inside the upload() method. Try logging req.body in the place where you print req.files.



来源:https://stackoverflow.com/questions/39354455/express-multer-bodyparser-req-body-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!