I am having trouble with this web application. I can enter all the data on the main page but when I enter it doesn’t appear on the list page. When I go into MongoDB the data is being stored. I have attached the server.js file and the petController.js file. I have also attached a few images of what I am seeing. I know it is an issue with the handlebars package, but I can’t seem to figure it out.
**server.js file**
require('./models/db');
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const hbs = require('express-handlebars');
const petController = require('./controllers/petController');
var app = express();
app.use(express.urlencoded({
extended: true
}));
app.use(express.json());
app.set('views', path.join(__dirname, '/views/'));
app.engine('handlebars', exphbs.engine({ extname: 'handlebars', defaultLayout:'mainLayout', layoutsDir: __dirname + '/views/layouts/' /*hanldlebars: allowInsecurePrototypeAccess(Handlebars)*/ }));
app.set('view engine','handlebars');
app.listen(3000, () => {
console.log('Express server started at port : 3000');
});
app.use('/pet', petController);
**petController.js**
const express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const Pet = mongoose.model('Pet');
router.get('/', (req, res) => {
res.render("pet/addOrEdit", {
viewTitle: "Insert Pet"
});
});
router.post('/', (req, res) => {
if (req.body._id == '')
insertRecord(req, res);
else
updateRecord(req, res);
});
function insertRecord(req, res) {
var pet = new Pet();
pet.petName = req.body.petName;
pet.breed = req.body.breed;
pet.age = req.body.age;
pet.owner = req.body.owner;
pet.save((err, doc) => {
if (!err)
res.redirect('pet/list');
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("pet/addOrEdit", {
viewTitle: "Insert Pet",
pet: req.body
});
}
else
console.log('Error during record insertion : ' + err);
}
});
}
function updateRecord(req, res) {
Pet.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true }, (err, doc) => {
if (!err) { res.redirect('pet/list'); }
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("pet/addOrEdit", {
viewTitle: 'Update Pet',
employee: req.body
});
}
else
console.log('Error during record update : ' + err);
}
});
}
router.get('/list', (req, res) => {
Pet.find((err, docs) => {
if (!err) {
res.render("pet/list", {
list: docs
});
}
else {
console.log('Error in retrieving pet list :' + err);
}
});
});
function handleValidationError(err, body) {
for (field in err.errors) {
switch (err.errors[field].path) {
case 'petName':
body['petNameError'] = err.errors[field].message;
break;
case 'breed':
body['breedError'] = err.errors[field].message;
break;
default:
break;
}
}
}
router.get('/:id', (req, res) => {
Pet.findById(req.params.id, (err, doc) => {
if (!err) {
res.render("pet/addOrEdit", {
viewTitle: "Update Pet",
pet: doc
});
}
});
});
router.get('/delete/:id', (req, res) => {
Pet.findByIdAndRemove(req.params.id, (err, doc) => {
if (!err) {
res.redirect('/pet/list');
}
else { console.log('Error in pet delete :' + err); }
});
});
module.exports = router;
**Images**
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[1]: https://i.stack.imgur.com/NQUWg.png
[2]: https://i.stack.imgur.com/9ZsCi.png
[3]: https://i.stack.imgur.com/GVW4Q.png
[4]: https://i.stack.imgur.com/muC2L.png