I'm new to using node js and I am currently attempting a restaurant booking system where the dates are stored in a mysql database table. I am currently trying to pull through these values (the dates) to a html page within elements.
So far, I have managed to connect to the mysql database and display the results array on the url http://localhost:1337/get-content within my database.js file.
I have then attempted to use jquery in a separate js file to display the results in a div element. I attempted this solution code in this thread: How to display nodejs mysql results on html page? but it just continues to display the results array.
var mysql = require("mysql");
var express = require("express");
var app = express();
var http = require("http");
var path = require("path");
var fs = require("fs"); //file assist
var datesArr = [];
app.get("/get-content", function (req, resp) {
db.query("SELECT Date FROM dates", function (error, results) {
if (error) {
console.log("Error in the query");
console.log(error);
} else {
console.log("successful query.");
resp.send(results);
}
});
console.log("Displaying Data!");
});
var db = mysql.createConnection({
//properties...
host: "localhost",
user: "root",
password: "",
database: "sampledb",
});
db.connect(function (error) {
//callback
if (error) {
console.log("Error");
} else {
console.log("Connected");
}
});
function executeQuery(sql, cb) {
db.query(sql, function (error, result, fields) {
if (error) {
throw error;
}
cb(result);
});
}
app.listen(1337);
jQuery.get("/get-content", function (data) {
jQuery.each(data, function (index, item) {
var status = item.status;
var div = "<div>" + status + "</div>";
$("#messages").append(div);
});
var x = "<div>END</div>";
$("#messages").append(x);
});
Is anyone able to advise me of how I can retrieve data from mysql data using node js and then display it within a html element (not a html table) within a html page using jquery? Thank you!