this would be my controller module , as i have already set up the router module
const Book = require('../models/book');
const Author = require('../models/author');
const Genre = require('../models/genre');
const BookInstance = require('../models/bookinstance');
const async = require('async');
exports.index = (req, res) =>{
async.parallel({
book_count(callback) {
Book.countDocuments({}, callback); // Pass an empty object as match condition to find all documents of this collection
},
book_instance_count(callback) {
BookInstance.countDocuments({}, callback);
},
book_instance_available_count(callback) {
BookInstance.countDocuments({ status:'Available' }, callback);
},
author_count(callback) {
Author.countDocuments({}, callback);
},
genre_count(callback) {
Genre.countDocuments({}, callback);
}
},
(err, results) => {
res.render('index', { title: 'Local Library Home', error: err, data: results });
});
};
Here im using pug as my rendering engine , it does display the title and the necessary elements but in the list section im trying to display my data which i have also included an if statement to indicate an error of retrieving the data, which it currently is. as if it was not connected to my database/model
extends layout
block content
h1= title
p Welcome to #[em LocalLibrary], a very basic website.
h1 Dynamic content
if error
p Error getting dynamic content.
else
p The library has the following record counts:
ul
li #[strong Books:] !{data.book_count}
li #[strong Copies:] !{data.book_instance_count}
li #[strong Copies available:] !{data.book_instance_available_count}
li #[strong Authors:] !{data.author_count}
li #[strong Genres:] !{data.genre_count}
which i've done all the necessary connection to the database
var Book = require('./models/book')
var Author = require('./models/author')
var Genre = require('./models/genre')
var BookInstance = require('./models/bookinstance')
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));