Multer: Unexpected Field error even though Form Data and upload.single values match

Created on 8 May 2018  路  13Comments  路  Source: expressjs/multer

I've read through 3 or 4 of the closed issues on this type of problem already, and the solution for all of them was that their form data and upload.single values were not matching. As far as I can tell, I've got matching values, but I'm still running into an Unexpected Field error. My project is a Vue.js/Express application:

Front End

<template>
  <div>
    <input type="file" @change="fileSelected">
    <button @click="onUpload">Upload Photo</button>
  </div>
</template>

<script>
import Axios from 'axios';

export default {
  name: 'PhotosForm',
  data() {
    return {
      msg: 'Welcome to Your Vue.js App',
      photo: null,
    };
  },
  methods: {
    fileSelected(event) {
      this.photo = event.target.files[0];
    },
    onUpload() {
      const formData = new FormData();
      formData.append('photo', this.photo, this.photo.name);
      Axios.post('http://localhost:4000/api/photos/', formData);
    },
  },
};
</script>

Back End

var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
var cors = require('cors');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(upload.array());
app.use(express.static('public'));
app.use(cors());

var port = 4000;

router.post('/photos', upload.single('photo'), function(req, res, next) {
  console.log(req.body);
});

app.use('/api', router);

app.listen(port);
console.log("Server started on " + port);

I've been trying to figure out where I am going wrong for 3 days. Read several tutorials online on how to user Multer. As far as I can tell, I'm using it correctly, but still getting the error. Any help you could provide would be greatly appreciated. Thanks!

Most helpful comment

Was the problem that you have misspelled one of the option names? I'm not sure how we could improve the documentation there 馃

All 13 comments

Can you use your browser's developer tools to see what gets sent over the wire?

with the same problem

@cddsgtc Can you use your browser's developer tools to see what gets sent over the wire?

@LinusU
I have solved it, just because the key words doesn't match the option key words.
maybe the doc need some detail information.

maybe the doc need some detail information.

I'd be happy to have it improved! What was it that got you?

@LinusU
im a newbie for node.js and also a coder that English is not my mother language.
what got me is that maybe it should be design like that.
im good at front-end programming.

Was the problem that you have misspelled one of the option names? I'm not sure how we could improve the documentation there 馃

Logging the unexpected field would help (sanitized, of course).

@LinusU
I have solved it, just because the key words doesn't match the option key words.
maybe the doc need some detail information.

Thanks, this solved it for me too. The docs really need to be updated.

@kestonorherhe

The docs really need to be updated.

Which part is wrong in the documentation? Would you be able to submit a PR to fix/improve it?

image
image
Getting the same error, I am testing on postman, I don't have a front end yet.

Sorry, I was also not passing the image name, it is now fine

Closing as resolved/inactive

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josephstgh picture josephstgh  路  3Comments

kiwenlau picture kiwenlau  路  4Comments

BlueOctober picture BlueOctober  路  3Comments

edi picture edi  路  4Comments

tjabdoullah picture tjabdoullah  路  4Comments