Bootstrap carousel doesn't work after I remove the active class - node.js

I made a page and I get from database an array of paths
eg:
images/file_name.jpg
and I want to display those images in a carousel from bootstrap
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel" >
<div class="carousel-inner">
<% for(let picture of modelpics) { %>
<div class="carousel-item">
<img class="d-block w-100" src="/<%= picture %>" >
</div>
<% }%>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
Data are fetched correctly from db. I'm working with EJS, NodeJS and Mongoose. Can you please tell me what should I do for this to work? I copied the code from Boostrap Page , but it's not working, I also deleted that active class, maybe that's why, but how can I programatically add new "Carousel Item " after that active class? If I do ' picture[0] ' it wont work in that " FOR ".
Thank you very much!

running in same issue, using node js, ejs and mysql. Getting individual arrays of paths representing images to show in a bs carousel.
Therefore you need the item-active and item class respectively two image, src classes. To differentiate while looping put in an "if - else" clause running a var like i==0 -> active class, else -> other items for indexing array inputs.
Hope this is a start.

works for me:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<% for (var pfad of players){%>
<% if (i==0) {%>
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<%} else {%>
<li data-target="#carouselExampleIndicators" data-slide-to="<% i %>"></li>
<!--<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>-->
<% } %>
<% i = i+1; %>
<% } %>
</ol>
<!--<ol class="carousel-indicators">-->
<div class="carousel-inner">
<% var i = 0; %>
<% for(var pfad of players){%>
<% if (i==0) {%>
<!--<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>-->
<div class="carousel-item active">
<img class="d-block w-100" src="<%= '/assets/img/' + pfad.image_gallery %>" alt="First slide">
</div>
<%} else {%>
<!--<li data-target="#carouselExampleIndicators" data-slide-to="<% i %>"></li>-->
<div class="carousel-item">
<img class="d-block w-100" src="<%= '/assets/img/' + pfad.image_gallery %>" alt="Second slide">
</div>
<% } %>
<% i = i+1; %>
<% } %>
</div>
<!--</ol>-->
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

Related

How do I add img from my database (mongodb) to a bootstarp carousel using for Each

I tried using forEach to loop through my database, and display images in a bootstrap carousel, but only the first image in the database is displaying along with other details pertaining to the img is showing, the rest of the images isn't and the carousel buttons aint working as well
below is my ejs code
<h5>Toyota Cars</h5>
<div id="car-slide" class="carousel slide" data-bs-ride="false">
<div class="carousel-indicators">
<button type="button" data-bs-target="#car-slide" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#car-slide" data-bs-slide-to="1" aria-label="Slide 2"></button>
<button type="button" data-bs-target="#car-slide" data-bs-slide-to="2" aria-label="Slide 3"></button>
</div>
<% toyotas.forEach((toyota, inx)=>{ %>
<div class="carousel-inner">
<% if (inx ==0){ %>
<div class= "carousel-item active">
<img src="<%= toyota.image%>" class="d-block w-100" id="car-caro"alt="...">
<div class="carousel-caption d-md-block">
<div class="arrow">
<div class="mil">
<p><i class="fa-solid fa-arrow-up-right-from-square" id="millage"></i></p>
<p class=""><%=toyota.millage %> </p>
<h6 class="car-name">
<%= toyota.carName %>
</h6>
<div class="car-properties">
<p><%= toyota.seater %> </p>
<p><%= toyota.mechnism %> </p>
<p><%= toyota.combustion %> </p>
</div>
</div>
</div>
</div>
</div>
<% }else { %>
<div class= "carousel-item">
<img src="<%= toyota.image%>" class="d-block w-100" id="car-caro"alt="...">
<div class="carousel-caption d-none d-md-block">
<div class="arrow">
<div class="mil">
<p><i class="fa-solid fa-arrow-up-right-from-square" id="millage"></i></p>
<p class=""><%=toyota.millage %> </p>
<h6 class="car-name">
<%= toyota.carName %>
</h6>
<div class="car-properties">
<p><%= toyota.seater %> </p>
<p><%= toyota.mechnism %> </p>
<p><%= toyota.combustion %> </p>
</div>
</div>
</div>
</div>
</div>
<% } %>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#car-slide" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#car-slide" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
<%})%>

error in nodejs using sripe for payment for website

I am implementing a payment method in my e-commerce website using stripe and node.js , and I am not using a database only using JSON files to add my items to the website, but I am getting errors in ejs file. for rendering the page I used the fs module in node js. I wanted to know where I can define the items in ejs , or server , or HTML file
''' shop.ejs'''
<div class="box-container">
<% items.products.forEach(function(item){ %>
<div class="box" data-item-id="<%= item.id %>">
<div class="icons">
</div>
<img class="image" src="/image/<%= item.imgName %>" alt="">
<div class="content">
<div class="price"> &#8377 <%= item.price %></div>
<h3 class="shop-item-title"><%= item.name %></h3>
<div class="stars">
<i class="fas fa-star"></i>
<i class="fas fa-star"></i>
<i class="fas fa-star"></i>
<i class="fas fa-star"></i>
<i class="far fa-star"></i>
<span> (50) </span>
</div>
</div>
</div>
<% }) %>
'''server.js'''
app.get('/shop', function(req, res){
fs.readFile('items.json', function(error, data){
if(error){
res.status(500).end()
}else {
res.render('shop.ejs', {
items: JSON.parse(data)
})
}
})
})
'''error'''
>> 172| <% items.products.forEach(function(item){ %>
173| <div class="box-container" data-item-id="<%= item.id %>">
174| <div class="box">
175| <div class="icons">
items are not defined
Did you pass the items data object to the ejs render function?

Node.js - rendering a undefined variable

Maybe the name of the title is wrong but I have a specific problem. The thing I want to do is to show possibilities of editing and deleting a post if a user is an admin. I have a local variable called currentUser that holds data specifically username, password, and role. This line of code works fine if the user is logged in but the problem that I have is when there is no data in local variable. if i give a statement if(currentUser == undefined or null) i still get a error and the site is not rendering(error that i get is role is not defined). Since I'm still new to this I believe currentUser data is coming from a middleware function from passport js. My question is how to render a page when currentUser is undefined.
Thanks in advance!
<% menu.forEach(function(menu){ %>
<div class="col-lg-4 col-md-6 col-12 p-2 m-0 d-inline-block">
<div class="card" style="width: 22rem; ">
<img class="card-img-top img-fluid" src="<%= menu.pictureUrl %>" alt="Card image cap">
<div class="card-body">
<div style="height: 9rem;">
<h5 class="card-title"><%= menu.meal %></h5>
<div style="height: 4rem;">
<p class="card-text"><%= menu.description %></p>
</div>
<div style="height: 4rem;">
<p class="card-text"><strong><%=menu.price %>,00 Kn</strong></p>
</div>
</div>
<% if(currentUser.role == undefined){ %>
Stavi u košaricu
<%} else if(currentUser.role === "admin"){ %>
Uredi
<div class="d-inline">
<form class="d-inline" action="/menu/<%=menu._id%>?_method=DELETE" method="post"><button
class="btn btn-danger">Obriši</button></form>
</div>
<%}%>
</div>
</div>
</div>
<% }); %>
</div>
</div>
</div>

How can I use JSON image file object on ejs

I want to use an image on my ejs code, but I use loop with JSON array, so I want to make image url to object of JSON.
I put the JSON object way on the source code.
{
"champion" :
[
{
"id": "266",
"key": "Aatrox",
"name": "Aatrox",
"origin": ["Demon"],
"class": ["Blademaster"],
"cost": 3,
"img" : "https://ddragon.leagueoflegends.com/cdn/9.17.1/img/champion/Aatrox.png",
This json file's local data name is "cdata".
<!-- Champion Data -->
<% for(var i=0; i<cdata.champion.length; i++){ %>
<div class="col-xl-12 col-md-6 mb-12">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-3"><h3><%= cdata.champion[i].name %></h3></div>
<img src= <%= "cdata.champion[0].img" %> class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
<div class="h5 mb-0 font-weight-bold text-gray-800"><%= cdata.champion[i].key %></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<% } %>
My website shows broken image https://ykyuen.files.wordpress.com/2015/10/svg-image-broken.png
after that, I changed the code to
<img src= "<%= cdata.champion[0].img %>" class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
and
<img src= <%= cdata.champion[0].img %> class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
but it still doesn't work.
You can simplify your code from using for loop to use .forEach Here is docs
<% cdata.champion.forEach(function(champion){ %>
<div class="col-xl-12 col-md-6 mb-12">
<div class="card border-left-primary shadow h-100 py-2">
<div class="card-body">
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-3"><h3><%= champion.name %></h3></div>
<img src="<%= champion.img %>" class="ChampionImage__ChampImage-xj39xo-2 fhAQxz">
<div class="h5 mb-0 font-weight-bold text-gray-800"><%= champion.key %></div>
</div>
<div class="col-auto">
<i class="fas fa-calendar fa-2x text-gray-300"></i>
</div>
</div>
</div>
</div>
</div>
<% }) %>
In my case, I've used <img src="<%= image %>" and it worked.
Of course, the code should work only in case of correctly parsed JSON.

showing MongoDB document value to EJS in a loop

I'm working on a node project. And node is very new to me so sorry if I ask a lot of stupid questions.
I'm trying to show all of my topics on my index.ejs. The root file already shows them in the console log but I can't seem to get them to show in the view.
The view already loops and knows that there are 2 topics, but the content is empty.
This is my code from the routes/index.js
router.get('/', function(req, res, next) {
Topic.find({}).exec(function(err, topic)
{
if(err){
console.log('There ware no topics');
return next(err)
}
else
{
console.log('Whoop whoop there are some topics');
res.render('index', { topic: topic } );
console.log("Logging data: " + topic);
console.log("Loggin data title out db: " + topic.topicTitle);
console.log("Loggin data desc out db: " + topic.topicDescription);
console.log("Loggin data date out db: " + topic.topicDateCreated);
}
});
});
module.exports = router;
And here is how I try to show it in my views/index.ejs
<ul>
<% for(var i = 0; i < topic.length; i++) {%>
<li>
<div class="post animated fadeInLeft">
<div class="wrap-ut pull-left">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="default avatar" />
<p class="moderator"> <%= topic.fbUsername %> </p>
</div>
</div>
<div class="posttext pull-left">
<h2 class="topictitle"><a href="/topicdetail/{topic_id}" <%= topic.topicTitle %> </a></h2>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</li>
<% } %>
</ul>
The topic variable is an array, so you need to pass the index in order to get the proper Topic document. For instance:
<%= topic[i].fbUsername %>
Your view should look like:
<ul>
<% for(var i = 0; i < topic.length; i++) {%>
<li>
<div class="post animated fadeInLeft">
<div class="wrap-ut pull-left">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="default avatar" />
<p class="moderator"> <%= topic[i].fbUsername %> </p>
</div>
</div>
<div class="posttext pull-left">
<h2 class="topictitle"> <%= topic[i].topicTitle %> </h2>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</li>
<% } %>
</ul>
UPDATE
You also forgot to close the tag here:
--------------------------------------------------------↴
<h2 class="topictitle"><a href="/topicdetail/{topic_id}" <%= topic[i].topicTitle %> </a></h2>
Closing the tag, I was able to see the titles:
<h2 class="topictitle"> <%= topic[i].topicTitle %> </h2>

Resources