My approach involves using MongoJS to retrieve data from a MongoDB database as shown in the code snippet below:
var db = mongojs('db', ['events']);
Subsequently, I have the following setup:
app.get('/', function(req, res){
db.events.find(function(err,docs){
res.render('index', {
title: 'Events',
events: docs
});
})
});
This method successfully retrieves the data and renders it using EJS as the view engine. The sample code in index.ejs looks like this:
<% events.forEach(function(event){ %>
<a><%= event.event_name %></a></br>
<a><%= event.event_description %></a>
<% }) %>
While everything is functioning properly with fetching data from one collection, my query now shifts to how I can fetch data from a separate collection within the same EJS page. Here's my attempt to modify app.js:
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 incorporating the group iteration in the EJS file:
<% groups.forEach(function(group){ %>
<p><a><%= group.group_name %></a></br>
<a><%= group.editor %></a></p>
<a><%= group.members %></a></p>
<% }) %>
The issue arises when 'groups' are referenced as undefined. Even after reordering the queries in app.js, similar problems persist. How can both collections be properly declared without causing conflicts or overwriting each other? Your assistance on resolving this matter would be greatly appreciated.