I have get method in my Express app, which renders a collection from my MongoDB called "Members". This works find, and I'm able to read the result on the url "/agileApp". However, I want to render another collection into the same URL - a collections called "Tasks". Is this possible? Here are my current code:
expressApp.get('/agileApp', function(request, response) {
MongoClient.connect(url,
{useUnifiedTopology: true},
function(error, db) {
if (error) {
throw error;
}
let databaseobject = db.db(dbName);
databaseobject.collection("Members").find().toArray(function (err, result) {
if (err) throw err;
response.render('agileApp', {
result: result,
});
db.close();
});
});
});
Just take a request parameter for deciding which collection to render and depending on that value render that collection.
expressApp.get('/agileApp', function(request, response) {
// take the collection to render as request parameter (this is form parameter, json also can be used)
let collectionToRender = request.params.collectionToRender
MongoClient.connect(url,
{useUnifiedTopology: true},
function(error, db) {
if (error) {
throw error;
}
let databaseobject = db.db(dbName);
// here it will query the mongodb for the collection which has been provided in the request
// if user enters Tasks then Tasks collection will be rendered
databaseobject.collection(collectionToRender).find().toArray(function (err, result) {
if (err) throw err;
response.render('agileApp', {
result: result,
});
db.close();
});
});
});
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.