Multer TypeError:字段.forEach不是一个函数

Multer TypeError: fields.forEach is not a function

本文关键字:一个 函数 TypeError 字段 forEach Multer      更新时间:2023-09-26

我在c9上使用node.js中的multer,试图创建一个元数据阅读器。我已经搜索了相当长的一段时间,并没有找到任何人遇到这个错误。错误是:

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)

和我得到它后,试图提交我的文件上传。我认为这意味着它没有正确安装,但我试过用几种方法重新安装。它看起来是最新的:

{  "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"
  }
}
下面是我的代码:

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 );
});

和前端:

<!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>

从upload.fields ()给出错误

TypeError: Cannot read property 'forEach' of undefined

我只想上传一个文件,但是如果我使用

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

控制台输出

undefined
{}

我希望我错过了一些简单的东西。我已经参考了所有这些主题,但还没有找到一个适合我的解决方案:

热点问题

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 issue thread

很简单。发帖五分钟后就找到了。我把它留在这里,以防有人犯同样的错误。

问题出在我的前端:

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

必须是

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

出现"Multer TypeError: fields的原因。forEach不是一个函数。错误是upload.fields(input-param)中间件输入参数应该是array.

如文档中所述。输入参数应该像这样:[{name: 'avatar', maxCount: 1}]

请参考文档:https://www.npmjs.com/package/multer

PS:我也得到这个错误,因为我忘记在输入参数中添加数组括号