我对multer包很新。甚至到nodejs :)。我正在构建NBA应用程序来掌握我的新知识。我复制了multer包示例并修改了一些细节以适合我的代码。但我得到了这个错误。我认为我给了足够的(甚至太多不必要的元素)。请帮我解决这个问题。
以下是我的明确代码:
app.post("/admin/add/team", upload.single('file'), function(req, res){
var file = __dirname + '/public/img/' + req.file.filename + '.svg';
fs.rename(req.file.path, file, function(err){
if (err) {
console.log(err);
res.send(500);
} else {
var newTeam = {
win: 0,
lose: 0,
teamName: req.body.teamName,
fullName: req.body.teamNameFull,
img: '/img/' + req.file.filename + '.svg'
};
Team.create(newTeam);
res.redirect("/admin/add/team");
}
});
});
以下是我的表格:
<div style="width: 30%" class="container">
<form action="/admin/add/team" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label for="team-full-name">Team name</label>
<input type="text" class="form-control" id="team-full-name" placeholder="Houston Rockets" name="teamNameFull">
</div>
<div class="form-group">
<label for="team-name-to-display">Team name to display</label>
<input type="text" class="form-control" id="team-name-to-display" placeholder="HOU" name="teamName">
</div>
<div class="form-group">
<label for="team-img">Team imageг</label>
<input type="file" class="form-control-file" id="team-img" name="file">
</div>
<button class="btn btn-success">Submit</button>
</form>
</div>
以下是错误:
**TypeError: Cannot read property 'filename' of undefined**
at C:\Users\lamborari\Desktop\mnba\app.js:171:50
at Layer.handle [as handle_request] (C:\Users\lamborari\Desktop\mnba\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\lamborari\Desktop\mnba\node_modules\express\lib\router\route.js:137:13)
at Array.<anonymous> (C:\Users\lamborari\Desktop\mnba\node_modules\multer\lib\make-middleware.js:53:37)
at listener (C:\Users\lamborari\Desktop\mnba\node_modules\on-finished\index.js:169:15)
at onFinish (C:\Users\lamborari\Desktop\mnba\node_modules\on-finished\index.js:100:5)
at callback (C:\Users\lamborari\Desktop\mnba\node_modules\ee-first\index.js:55:10)
at IncomingMessage.onevent (C:\Users\lamborari\Desktop\mnba\node_modules\ee-first\index.js:93:5)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)