Я пытаюсь загрузить файл в облачный каталог, но после отправки формы выдает путь неопределенной ошибки. он показывает req.file как неопределенный. как я могу получить путь к файлу? ошибка
undefined
TypeError: Cannot read property 'path' of undefined
at D:\college\project web\project1 - Copy final think so - Copy\routes\campgrounds.js:49:44
at Layer.handle [as handle_request] (D:\college\project web\project1 - Copy final think so - Copy\node_modules\express\lib\router\layer.js:95:5)
at next (D:\college\project web\project1 - Copy final think so - Copy\node_modules\express\lib\router\route.js:137:13)
at middlewareo.isloggedin (D:\college\project web\project1 - Copy final think so - Copy\middleware\index.js:48:16)
почтовый маршрут
router.post("c/n/v/n", middleware.isloggedin, function(req, res) {
console.log(req.file)
cloudinary.v2.uploader.upload(req.file.path, { resource_type: "video" }, function(error, result) { console.log(result, error); });
});
форма
<form action="/c/n/v/n" method="post" enctype="multipart/form-data">
<h2 style="text-align: center;">Add video</h2>
<div style="width: 30%; margin: 20px auto;">
<div class="form-group"><input class="form-control" type="text" name="cat[name]" placeholder="name">
</div>
<div class="form-group">
<input class="form-control" type="number" name="cat[price]" placeholder="Price">
</div>
<div class="form-group">
<input type="file" name="file" accept="video/*" required>
</div>
<div class="form-group">
<input type="text" name="cat[description]" class="form-control" placeholder="description">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">submit!!</button></div>
<a href="/c">Go back</a>
</div>
</form>
РЕДАКТИРОВАТЬ, если video1: [{url: string, format: string}] для нескольких файлов
router.post("/n/v/n", upload.array('images'), middleware.isloggedin, async function(req, res) {
try {
req.body.ca.video1 = [];
for (var i = 0; i < req.files.length; i++) {
console.log(req.files[i].path);
cloudinary.v2.uploader.upload(req.files[i].path, { resource_type: "auto" }, function(error, result) {
req.body.ca.video1.push({
url: result.secure_url,
format: result.format
});
req.body.ca.author = {
id: req.user._id,
username: req.user.username
}
req.body.ca.created = new Date();
////if i get out of this then req.body.ca becomes undefined
const campground = await Campground.create(req.body.ca);
});
/////here the video1 goes undefined
}
req.flash("success", "it is uploading");
res.redirect("/c");
} catch (err) {
req.flash("error", err.message);
res.redirect("back");
}
});