Node.js MySQL module - throw err; // Rethrow non-MySQL errors;

拜拜、爱过 提交于 2019-12-10 07:01:26

问题


today I tried node.js mysql snippet from w3schools:

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 wanted to learn how to handle mysql errors, because my app require mysql. But after I started app.js file this error showed up. Why can't I throw an error?


回答1:


I wanted to learn how to handle mysql errors, because my app require mysql.

MySQLJS Error handling

To catch the errors you throw, try with the following snippet :

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


来源:https://stackoverflow.com/questions/45787211/node-js-mysql-module-throw-err-rethrow-non-mysql-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!