Reputation: 45
I am trying to make a bot for Slack that handles maintenance requests from around the office and stores them in a database using nodejs.
The front end works fine and the bot logs requests correctly, I've written the following code to display the database to a simple webpage :
exports.serveHTML = function serveHTML() {
var http = require('http');
http.createServer(function (req, res) {
var html = buildHtml(req);
res.writeHead(200, {
'Content-Type' : 'text/html',
'Content-Length' : html.length,
'Expires' : new Date().toUTCString()
});
res.end(html);
}).listen(8080);
function buildHtml(req) {
var header = '<html><head><title>Maintenance Log</title></head><body><h1>Maintenance Log</h1><table border="1"><tr><th>Problem Description</th><th>User</th> <th>ID</th><th>Date</th><th>Status</th> </tr>';
var body = '';
for (var i = 0; i < problemList.length; i++) {
body+="<tr>";
body+="<td>" + problemList[i].description + "</td>";
body+="<td>" + problemList[i].complainer + "</td>";
body+="<td>" + problemList[i].id + "</td>";
body+="<td>" + problemList[i].dateAndTime + "</td>";
body+="<td>" + problemList[i].status + "</td>";
body+="</tr>";
}
return '<!DOCTYPE html>'
+ header + '</header><body>' + body + '</body></html>';
};
};
This works to simply display the items but I want to include a radio button on each item so that the HR team can change to status of items.
I'm stuck on how to get this change to reflect on the database.
So my question is : how do I pass information back to the database from this page?
Upvotes: 1
Views: 1605
Reputation: 927
Well it depends on what type of data base you are using. If you guys are using MongoDb it would be fairly easy to get that data in there.
I would recommend looking into mongoose which makes it easy to save and retrieve data from MongoDb http://mongoosejs.com/
Also you could have a form on the page so that when they click submit it will send the data to your Node server.
<form action="/submit" method="get">
Description : <input type="text" name="description"><br>
Status: <input type="text" name="status"><br>
<input type="submit" value="Submit">
</form>
If you're using express on the back end you can easily grab the data then save it to MongoDb.
var Ticket = mongoose.model('Ticket', { status: String, description: String });
app.post('/submit', function(req, res) {
var ticketStatus = req.body.status;
var ticketDescription = req.body.description;
var ticket= new Ticket({ status: ticketStatus , description: ticketDescription });
ticket.save(function (err) {
if (err) // ...
console.log('saved');
});
});
Upvotes: 1