问题
I am trying to upload the file but there is 'TypeError: Cannot read property 'path' of undefined'. Is there any way to solve this?
exports.postimport = function (req,res) {
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads');
},
filename: function(req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage,
}).single('sheet');
upload(req, res, function(err) {
if (err) {
res.send(err)
} else {
console.log(req.file.path);
}
});
}
回答1:
The reason you might be getting path of undefined may be that your destination directory (i.e path to your uploads folder) is not correct.
来源:https://stackoverflow.com/questions/55864278/how-to-fix-typeerror-cannot-read-property-path-of-undefined-in-nodejsmulte