I am trying to display a specific field of the first object that is inside a array of objects.
index.js:
router.get('/chatroom', ensureAuthenticated, (req, res) => {
const user = req.user
chatroom.find({chatroom_users: user.name}, function (error, data) {
res.render("chatroom", {
data,
user: req.user});
})
}
})
chatroom.ejs:
<script>
<% for (var i = 0; i < data.length; i++) { %>
var chatroom_message0 = "<%= data[i].chatroom_message.date%>"
<% } %>
</script>
This is the structure in MongoDB:
But I can only so far show the entire object but not the specific field that I want, in this case the field "date".
I have tried many different combinations for example:
"<%= data[i].chatroom_message["date"]%>"
"<%= data[i].chatroom_message._date%>"
But I can’t get it to work, so any help would be appreciated.
2
Answers
I figured it out, I made a typo mistake in my mongodb model I had written "dates"in the model instead of "date" thats why it didn't show up.
the date is inside a array so it makes sense that you can’t select like that
depending on your data format it can be used like this:
if the format is
or if the format is