0
我在c9上使用mdter在node.js上使用express,嘗試創建元數據讀取器。我搜索了很長時間,找不到遇到這個錯誤的人。錯誤是:Multer TypeError:fields.forEach不是函數
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
multer-configuration-with-app-use-returns-typeerror