Я пытаюсь загрузить видео в свое приложение через облачный сервис, но каждый раз он выдает внутреннюю ошибку сервера, а в консоли выдает [объект Object]. это работает для изображений, но не для видео. ошибка в логах
2020-04-28T04:44:52.873926+00:00 app[web.1]: [object Object]
ReferenceError: /app/views/campgrounds/show.ejs:15
2020-04-28T04:59:45.914751+00:00 app[web.1]: 13| <div class="col-md-9">
2020-04-28T04:59:45.914752+00:00 app[web.1]: 14| <div>
2020-04-28T04:59:45.914752+00:00 app[web.1]: >> 15| <%for (var i=0;i<campgrounds.images.length;i++){%>
2020-04-28T04:59:45.914753+00:00 app[web.1]: 16| <%if(campgrounds.images[i].format==="mp4"){%>
2020-04-28T04:59:45.914757+00:00 app[web.1]: 17| <video src="<%- campgrounds.images[i].url%>" controls>
2020-04-28T04:59:45.914757+00:00 app[web.1]: 18| </video>
2020-04-28T04:59:45.914758+00:00 app[web.1]:
2020-04-28T04:59:45.914758+00:00 app[web.1]: campgrounds is not defined
облачный индекс
const cloudinaryStorage = require("multer-storage-cloudinary");
const storage = cloudinaryStorage({
cloudinary,
folder: 'ezama',
allowedFormats: ['jpeg', 'jpg', 'png', 'gif', 'mp4'],
filename: function(req, file, cb) {
let buf = crypto.randomBytes(16);
buf = buf.toString('hex');
let uniqFileName = file.originalname.replace(/\.jpeg|\.jpg|\.png|\.gif|\.mp4/ig, '');
uniqFileName += buf;
cb(undefined, uniqFileName);
}
});
module.exports = {
cloudinary,
storage
}
схема
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
price: String,
images: [{ url: String, public_id: String, format: String }],
format: String,
description: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
created: { type: Date, default: Date.now },
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});
module.exports = mongoose.model("Campground", campgroundSchema);
почтовый маршрут
router.post("/", middleware.isloggedin, upload.array('images'), async function(req, res) {
try {
req.body.ca.images = [];
for (const file of req.files) {
req.body.ca.images.push({
url: file.secure_url,
public_id: file.public_id,
format: file.format
});
}
req.body.ca.author = {
id: req.user._id,
username: req.user.username
}
req.body.ca.created = new Date();
const campground = await Campground.create(req.body.ca);
// console.log(campground);
res.redirect('/c/' + campground.id);
} catch (err) {
req.flash("error", err.message);
res.redirect("back");
}
});
форма через который я представляю
<div class="container">
<form action="/c" method="post" enctype="multipart/form-data">
<h2 style="text-align: center;">Add new entries</h2>
<div style="width: 30%; margin: 20px auto;">
<div class="form-group"><input class="form-control" type="text" name="ca[name]" placeholder="name">
</div>
<div class="form-group">
<input class="form-control" type="number" name="ca[price]" placeholder="Price">
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image" name="images" accept="image/*,video/*" multiple required>
</div>
<div class="form-group">
<input type="text" name="ca[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>
</div>
шоу e js
<%for (var i=0;i<campgrounds.images.length;i++){%>
<%if(campgrounds.images[i].format==="mp4"){%>
<video src="<%- campgrounds.images[i].url%>" controls></video>
<%} else {%>
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="<%= campgrounds.images[0].url%>" class="d-block w-100" alt="...">
</div>
<%for (var i=1;i<campgrounds.images.length;i++){%>
<div class="carousel-item">
<%-campgrounds.images[i].format%>
<img src="<%- campgrounds.images[i].url%>" class="d-block w-100" alt="...">
</div>
<%}%>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<%}%>
<%}%>
образец облачного ответа
{
"public_id": "ygzxwxmflekucvqcrb8c",
"version": 1427018743,
"signature": "4618ba7c3461b6531cb9d2f16b06ce672af793b6",
"width": 854,
"height": 480,
"format": "mp4",
"resource_type": "video",
"created_at": "2017-03-22T10:05:43Z",
"tags": [ ],
"bytes": 9094354,
"type": "upload",
"etag": "7e3977ca45a2c2a063e4f29fa3ecdfdd",
"url": "http://res.cloudinary.com/demo/video/upload/v1427018743/ygzxwxmflekucvqcrb8c.mp4",
"secure_url": "https://res.cloudinary.com/demo/video/upload/v1427018743/ygzxwxmflekucvqcrb8c.mp4"
}