NodeJS Multer类型错误:fields.forEach不是函数

pgky5nke  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(215)

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

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"
  }
}

下面是我的代码:

应用程序.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
{}

我希望我错过了一些简单的东西。我已经参考了所有这些主题,但还没有找到一个解决方案,对我来说:
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

cnjp1d6j

cnjp1d6j1#

很简单。发帖五分钟后找到的。我把它放在这里以防有人犯同样的错误。
问题出在我的前端:

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

需要

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

des4xlb02#

“Multer类型错误:fields.forEach不是函数。”错误为上载。fields(输入参数)中间件输入参数应为数组。
如文档中所述,输入参数应如下所示:[{名称:“虚拟形象”,最大计数:1 }]
请参考文档:https://www.npmjs.com/package/multer
PS:我也得到这个错误,因为我忘记在输入参数中添加数组括号。

相关问题