Reputation: 29
I'm trying to render a mongoDB object but it's not doing what I'm trying to accomplish.
My code (ejs):
<% for(let date in data) { %>
<% let parsedDate = Date.parse(date); %>
<% let totalTime = 0; %>
<% if(isNaN(parsedDate)) { %>
<p> <%= date %> is not date </p>
<% } else { %>
<p> <%= date %> is a date </p>
<% for(subject in data[date]) { %>
<p> subject: <%= subject %> time: <%= data[date][subject].time %> description: <%= data[date][subject].description %> </p>
<% totalTime += parseInt(subject.time) %>
<% } %>
<p> <%= totalTime %></p>
<% } } %>
The output:
As you can see nothing but a 0 shows up. Why even does the 0 show up? The objects name is "Test".
What my database looks like:
Upvotes: 0
Views: 30
Reputation: 29
I figured it out.
This is the correct code:
<% for(date in data) { %>
<% let parsedDate = Date.parse(date); %>
<% let totalTime = 0; %>
<% if(isNaN(parsedDate)) { %>
<p> <%= date %> is not date </p>
<% } else { %>
<p> <%= date %> is a date </p>
<% for(subject in data[date]) { %>
<% for(element in data[date][subject]) { %>
<p> subject: <%= data[date][subject][element].name %> time: <%= data[date][subject][element].time %> description: <%= data[date][subject][element].description %> </p>
<% totalTime += parseInt(data[date][subject][element].time) %>
<% } } %>
<p> <%= totalTime %></p>
<% } } %>
Upvotes: 0
Reputation: 76
you already have the date value in the loop to access info from it use date.date or date.time, or to loop on properties instead using data[date][subject] use for(subject in data[date]) and then access info using subject.time
Upvotes: 1