I'm fairly new to node.js and MongoDB so bear with me. I have a form which stores details into a MongoDB collection; I then query this using .findOne (for now). I'm basically trying to pass this value into my index.ejs file and display it as part of the table. So the table should appear:
Name: Connor in the web browser.
My code for querying the database:
router.get('/', function(req, res) {
res.render('admin/index');
MongoClient.connect("mongodb://localhost:27017/tickets", function(err, db) {
// Ensure we have connected
if(err) {
console.log("Cannot connect to database");
} else {
console.log("Connected to database");
}
// Create a collection to query
var collection = db.collection('tickets');
collection.findOne({name:String}, function(err, item) {
// Ensure we have found the ticket
if(err) {
console.log("There was a problem finding the ticket.");
} else {
console.log("Ticket found!");
}
});
});
});
My code generating the table:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>