I'm trying to get display users name from mongodb and I figured out how to do so, but now I want to design it for the admin panel and I want to display each user in a separated div
"The code below it works fine with me, all I need to know is how to set each name in a div"
here is a snippet from the ejs code
<h4>
Users </h4>
<% for(let i=0; i<Users.length; i++) {%>
<ul> <li> <%= Users[i].name %></li></ul>
<% } %>
</div>
</div>
</div>`
Related
I have an array of secret in user and I want to check if secret have only one element then simply print it in a paragraph tag but if secret have more elements then loop through all of them and show them on screen.
<%for(var i=0;i<userWithSecrets.length;i++){ %>
<% if(user[i].secret.length==1){%>
<p class="secret-text"><%=user[i].secret%></p>
<%}else{%>
<%for(var j=0;j<user[i].secret.length;j++){ %>
<p class="secret-text"><%=user[i].secret[j]%></p>
<%}%>
<%}%>
<%}%>
I am using express framework in node js.
for page rendering i use ejs.
given below route i passed 3 variable doctors title or loggined user id
router.get('/doctors/:id',passportConf.isAuthenticated, function(req,res,next){
Doctor.find({category:req.params.id})
.populate("category")
.exec(function(err, doctors){
if (err) return next (err);
res.render('main/doctor', {doctors: doctors, title:'doctors', id: req.user._id})
})
});
now problem is when i access these variable like this then i get my variable data
<%= doctors[i]._id%> or <%= title %> or <%= id%>
but when i acces these variable data in if statment then id data give nothing
i tried this in if statment
user id is 5cfaa0e32e21cb3c88885260
if (doctor[i]._id != '5cfaa0e32e21cb3c88885260')
then my code worked fine
if (id != '5cfaa0e32e21cb3c88885260')
thene my code not work
my ejs code
<% for(i=0; i<doctors.length; i++) {%>
<small><%= doctors[i].category.name%></small>
<ul>
<% if (id != '5cfaa0e32e21cb3c88885260') {%>
<form method="post">
<input class="form-control" type="hidden" name="doctor_id" value="<%= doctors[i]._id%>">
<button class="btn_1 medium" type="submit">Book now</button>
</form>
<%}%>
</ul>
</div>
</div>
<%}%>
I am trying to get for Each to enumerate through an object's properties and show up unique results. For example I'm trying to iterate through all the messages in a server and only display those where the current user is a sender
. And while doing that I want, to pull up all the receivers, create a group for each and display every message that interacts with that receiver. However I can only get to the point where I am displaying all messages but if a receiver comes twice it will create two separate groups with the same receiver instead of one.
I've tried to enumerate through the properties, but I get an error saying that you cannot apply a for Each on an object's properties.
<% message.forEach(function(message){%>
<% message.sender.id.forEach(function(message){ %>
<% if(currentUser._id.equals(message.sender.id)) {%>
<div>
<p>Message sent to <%= message.receiver.username %></p>
<ul>
<li><%= message.text %></li>
</ul>
</div>
<% }); %>
<%}%>
<%});%>
I'm expecting a div with each receiver and its correspondence instead I get multiple groups(one group per message found with the same receiver
I think you misplaced a bracket. Please try code below.
suggested code
<% message.forEach(function(message){%>
<% message.sender.id.forEach(function(message){ %>
<% if(currentUser._id.equals(message.sender.id)) {%>
<div>
<p>Message sent to <%= message.receiver.username %></p>
<ul>
<li><%= message.text %></li>
</ul>
</div>
<%};%> //I moved a bracket from this line to the next line
<%})%>
<%});%>
NodeJS, Mongoose
<% for(var i = 0; i < articles.length; i++){ %>
<ul class="list-group">
<li class="list-group-item">
<a href="/article/<%=articles._id%>">
<%= articles[i].title %>
</a>
</li>
</ul>
<% } %>
<% include partials/html-footer %>
How do I make it go to /articles/articles._id? I've tried -
<a href="/article/"+<%=articles._id%>>
as well. If there's anything else you need to see, let me know. Don't know what else to post.
You are missing the array index:
<a href="/article/<%= articles[i]._id %>">
I created an app following this tutorial (without scaffolding).
After I create an item I can click on it and it shows me a big list of parameters. Like here: http://s15.postimage.org/j6at9koiz/parameters.png .
The code which does that is:
<% if (todos && todos.length) { %>
<% for (var i in todos) { %>
<div class="row todo-item">
<div class="span8">
<h3><%- linkTo(todos[i].title, todoPath(todos[i].id)) %></h3>
</div>
<div class="span4"><h3><i class="icon-list-alt"></i><%= todos[i].status; %></h3></div>
</div>
<% } %>
<% } %>
To be more specific, the following line is the one which displays the links with the titles which take me to the list of the parameters for each item:
<%- linkTo(todos[i].title, todoPath(todos[i].id)) %>
Can I do something to display only some of the parameters and not the entire list which is displayed now?
Thank you!
You need to add view files for todo resource. If you're scaffolding, then geddy creates them by default. But otherwise, you have to add view files for todo in app/views/todos.
View files
_form.html.ejs
edit/new form
add.html.ejs
new resource view
/todos/add
edit.html.ejs
edit view
/todos/:id/edit
index.html.ejs
index view
/todos
show.html.ejs
show individual resource
/todos/:id
You can edit them manually. For changing how a individual todo item should appear on /todos/:id route, edit show.html.ejs
<div class="hero-unit">
<%- linkTo('Edit this todo', editTodoPath(params.id), {class: 'btn pull-right'}); %>
<h3>Params</h3>
<ul>
<li>todo.title</li>
<li>todo.property1</li>
<li>todo.property2</li>
</ul>
</div>