我在node.js中使用multer,在c9上使用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"
}
}
这是我的代码:
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
{}
我希望我遗漏一些简单的东西。我已经引用了所有这些主题,还没有找到适合我的解决方案:
express-multer-cannot-read-property-profileimage-of-undefined
multer-node-eacces-error-on-c9-io
答案 0 :(得分:1)
非常简单。发布后五分钟发现。如果有人犯了同样的错误,我会把它留在这里。
问题出在我的前端:
<input type="file" id="fileUpload"></input>
需要
<input type="file" id="file"></input>