2

I am using multer in node.js with express on c9, trying to create a metadata reader. I've searched for quite some time, and can't find anyone who has run into this error. The error is:

TypeError: fields.forEach is not a function
at Multer.setup (/home/ubuntu/workspace/node_modules/multer/index.js:29:12)
at multerMiddleware (/home/ubuntu/workspace/node_modules/multer/lib/make-middleware.js:20:19)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
at serveStatic (/home/ubuntu/workspace/node_modules/express/node_modules/serve-static/index.js:74:16)

and I get it after trying to submit my file for upload. I would assume that this means it didn't install correctly, but I've tried re-installing in a few ways. It looks up-to-date:

{  "name": "workspace",


"version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.13.4",
    "jade": "~1.11.0",
    "morgan": "~1.7.0",
    "multer": "^1.2.0",
    "serve-favicon": "~2.3.0"
  }
}

Here is my code:

app.js

var express = require('express');
var app = express();
var path = require('path');
var multer  = require('multer');
var upload = multer({ dest: __dirname + '/public/uploads/'});
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.post('/file-upload', upload.fields('files'), function (req, res, next) {
  console.log(req.files);
  console.log(req.body);
});

app.listen(process.env.PORT, function () {
  console.log('app listening on port ' + process.env.PORT );
});

and the front end:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<html>
  <body>
    <div align='center'>
      <form method="post" enctype="multipart/form-data" action="/file-upload" name="upload">
      <h1>Upload a file</h1>
      <input type="file" id="fileUpload"></input>
      <h2>View its Metadata</h2>
      <button type="submit" value="submit" id="submit">submit</button>
      </form>
    </div>

  </body>
</html>

removing 'files' from upload.fields() gives the error

TypeError: Cannot read property 'forEach' of undefined

I am only trying to upload a single file, but if I use

app.post('/file-upload', upload.single('file'), function (req, res, next) {
  console.log(req.file);
  console.log(req.body);
});

// or upload.single()

My console outputs

undefined
{}

I'm hoping that I'm missing something simple. I've referenced ALL of these topics, and haven't yet found a solution that has worked for me:

hot multer issues

req files undefined

nodejs-multer-is-not-working

express-multer-cannot-read-property-profileimage-of-undefined

multer-callbacks-not-working

multer-node-eacces-error-on-c9-io

multer-configuration-with-app-use-returns-typeerror

Github multer issues thread

2 Answers 2

0

Was very simple. Found it five minutes after posting. I'll leave it here in case someone makes the same mistake.

The problem was in my front end:

<input type="file" id="fileUpload"></input>

needed to be

<input type="file" id="file"></input>
0
0

The reason for "Multer TypeError: fields.forEach is not a function." error is upload.fields(input-param) middleware input paramter which should be array.

As mentioned in the docs. The input parameter should look like this: [{ name: 'avatar', maxCount: 1 }]

Please refer to the docs: https://www.npmjs.com/package/multer

PS: I also get this error because I forgot to add the array bracket in the input parameter.

Not the answer you're looking for? Browse other questions tagged or ask your own question.