Reputation: 45
I'm trying to inject my MongoDB data to the HTML, but i keep getting this: "Error: each doesn't match else", I tried to change a few lines but i still getting this error, it's my first project with MongoDB or databases. What to i need to change in order my code to work? is it HTML or node problem?
HTML
{{#each ideas}}
<div>
<p>{{tip}}</p>
</div>
{{/else}}
server.js
const path = require('path');
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser')
const mongoose = require('mongoose');
const handlebars = require('handlebars')
const app = express();
//map global promise - get rid of warning
mongoose.Promise = global.Promise;
// connect to mongoose
mongoose.connect('mongodb://localhost/peppino-calc', {
useMongoClient: true
})
.then(() => { console.log('MongoDB connected...')})
.catch(err => console.log(err));
//Load salaryModel
require('./modles/Idea.js');
const Idea = mongoose.model('ideas');
//handlebars middleware
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
handlebars.registerHelper("toLowerCase", function(input) {
var output = input.toLowerCase();
return output.replace(" ", "");
});
//body parser middleware
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
// post history page
app.get('/history', (req, res) => {
Idea.find({})
.sort({date: 'desc'})
.then(ideas => {
res.render('../js/newJs/history', {
ideas:ideas
})
});
})
//process form
app.post('/ideas', (req, res) => {
let errors = [];
if(errors.length > 0) {
console.log(errors[0]);
} else {
const newUser = {
amount: req.body.totalamount,
hours: req.body.totalhours,
salary: req.body.totalsalary,
tip: req.body.totaltip,
date: req.body.datetotal
}
new Idea(newUser)
.save()
.then(idea => {
res.redirect('/history');
})
}
});
app.use(express.static(path.join(__dirname, './js/newJs')));
app.set('port', process.env.PORT || 5700);
var server = app.listen(app.get('port'), function() {
console.log('listening on port ', server.address().port);
});
Upvotes: 0
Views: 2638
Reputation: 239
I know you should have fixed this by now, But I want to answer this just for people that might run into this type of error in the future.
Looking at your code, the error is in your HTML file (Handlebars syntax) not server.js.
Your {{#each}}
iterator should close with {{/each}}
not {{/else}}
.
So your code should look like this:
{{#each ideas}}
<div>
<p>{{tip}}</p>
</div>
{{else}}
<div>
<p>Do nothing</p>
</div>
{{/each}}
Upvotes: 1
Reputation: 74909
The error is from Handlebars, an #each
iterator should be closed with /each
{{#each ideas}}
<div>
<p>{{tip}}</p>
</div>
{{/each}}
else
can be used for when there is an empty list, if it is inside the each
section:
{{#each ideas}}
<div>
<p>{{tip}}</p>
</div>
{{else}}
<div>
<p>none</p>
</div>
{{/each}}
Upvotes: 3