简体   繁体   中英

how to render multiple collections data to page in node js from mongodb

I am having problems rendering multiple collection to page.I have done a lot of research but getting an error like Failed to look up view in views directory my code is following

router.get('/', function(req, res, next) {
MongoClient.connect(url, function (err, db) {
if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
db.collection('book', function(err, collection1) {
collection1.find().toArray(function(err, items) {
db.collection('category', function(err, collection) {
collection.find().toArray(function(err, citems) {
res.send('pages/home', {title:'Express',itm:items,citm:citems});
});
});
});
});
 }
});

在此处输入图片说明 在此处输入图片说明

the problem with your view location, it's cannot be found by path pages/home

Try to debug this first and don't think about mongodb now. But if you want to debug mongodb queries - just log into console your items and citems

In app.js you can set the views to view like this:

app.set('views', [__dirname + 'views', __dirname + 'views/pages']);

and your response to:

res.render('home',{title:'Express',itm:items,citm:citems});

try this (I am assuming you are using express.js and jade)

res.render('./pages/home', {title:'Express',itm:items,citm:citems});

In app.js, please add the following piece of code,

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');

Hope this helps, Thanks..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM