Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

Today I have tried node.js MySQL snippet from a site:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "roots", // WRONG USER
  password: ""
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  /*Create a database named "mydb":*/
  con.query("CREATE DATABASE mydb", function (err, result) {
    if (err) throw err;
    console.log("Database created");
  });
});

I needed to learn how to handle MySQL errors because my app needs MySQL. However, later I started app.js file this following error displayed:

image

Why can not I throw an error?

1 Answer

0 votes
by (12.7k points)

If you want to catch the errors that you throw, you can try the below snippet:

con.on('error', function(err) {
  console.log("[mysql error]",err);
});

To learn more about SQL, you can join this SQL Certification course curated by Intellipaat.

Further, you can go through this MySQL Tutorial video for a better understanding.

Related questions

...