0

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});
});
});
});
});
 }
});

enter image description here enter image description here

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

i am getting items and citems afer printing them in console but when i pass them in page to render values i get page views look up error
console.log(items,citems) // res.render('./pages/home', { title: 'Express',itm:items,citm:citems}); it works if i uncomment it but not able to render it to page
show your files structure tree and where you initialize view engine for express. Something with your pathes
i have attached an image showing file structure. i have used ejs engine to show views templates
ok, @NarendraSharma, the problem is with view named 'error', cause your request generatin error and express trying to lookup 'error.ejs'. Maybe problem inside your home.ejs structure or code. Try to delete all content from home.ejs and look at the backend logs, if it's generating error or not
1

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});

2 Comments

once try creating error.ejs file in your views directory instead or error.jade.
i tried creating error.ejs but still getting the same error msg
0

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

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

1 Comment

no luck big bro . i am still getting an error it works great by doing so but not when uncommenting render page console.log(items,citems) // res.render('./pages/home', { title: 'Express',itm:items,citm:citems});
0

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..!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.