Even after looking at how-to-iterate-over-array-of-objects-in-handlebars, I could not make it work. I am using mongoose model, and want to simply iterate over objects and display both title and details:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const IdeaSchema = new Schema({
title: {
type: String,
required: true
},
details: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
mongoose.model('ideas', IdeaSchema);
Here is my handlebars template:
{{#each ideas}}
<div class="card card-body">
<h4>{{title}}</h4>
<p>{{details}}</p>
</div>
{{/each}}
It displays 2 empty divs with no content inside:
<div class="card card-body">
<h4></h4>
<p></p>
</div>
<div class="card card-body">
<h4></h4>
<p></p>
</div>
Here is the code that loads the collection
Idea.find({})
.sort({ date: 'desc' })
.then(ideas => {
res.render('ideas/index', {
ideas: ideas
});
});