Despite having correct server credentials this segment of code being called from a function never reaches the alert at the bottom of the snippet nor does it provide any error message despite the inclusion of a throw err statement. It reaches the alert saying "PING" but never even executes the "PONG" alert. What is wrong here? In MySQL Workbench the credentials seem to be correct and I've inserted data into columns using that environment just fine.
alert("PING");
//Begin SQL query
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : 'password',
port: 3306,
database : 'db'
});
connection.connect();
connection.query('SELECT * FROM test', function(err, rows, fields)
{
if (err) throw err;
console.log(rows[0]);
});
connection.end();
alert("PONG");
alert()function in node? Are you running this in a browser? If so, you'll get an error message for usingrequire. Node JavaScript and browser JavaScript are not the same. Requiring modules like that and accessing a DB directly is only possible with node.alertstatements and run this from the command line usingnode test.js. If you see an alert displayed, you're running this as <script> in a browser. Howevermysqlis a node module, i.e. backend.