I am using MongoJS to fetch data from mongo db in the following way: app.js:
var db = mongojs('db', ['events']);
And then:
app.get('/', function(req, res){
db.events.find(function(err,docs){
res.render('index', {
title: 'Events',
events: docs
});
})
});
This method works, I then fetch it using ejs as the view engine the following way: index.ejs:
<% events.forEach(function(event){ %>
<a><%= event.event_name %></a></br>
<a><%= event.event_description %></a>
<% }) %>
This all works, however, How could I grab data from a different collection in the same ejs page? I've been trying to do it the following way: app.js: edited into
var db = mongojs('db', ['events', 'groups']);
And then:
app.get('/', function(req, res){
db.events.find(function(err,docs){
res.render('index', {
title: 'Events',
events: docs
});
})
});
app.get('/', function(req, res){
db.groups.find(function(err,docs){
res.render('index', {
title: 'Groups',
groups: docs
});
})
});
Followed by the EJS:
<% groups.forEach(function(group){ %>
<p><a><%= group.group_name %></a></br>
<a><%= group.editor %></a></p>
<a><%= group.members %></a></p>
<% }) %>
The error that I'm getting is that 'groups' is not defined, however if I flip them like this inn app.js:
app.get('/', function(req, res){
db.groups.find(function(err,docs){
res.render('index', {
title: 'Groups',
groups: docs
});
})
});
app.get('/', function(req, res){
db.events.find(function(err,docs){
res.render('index', {
title: 'Events',
events: docs
});
})
});
Then it says events is not defined. How do I declare them both in one without one overwritting the other? If you can help with this, thank you so much.