Node.js MySQL模块 - 抛出错误; //重新抛出非MySQL错误;

问题描述 投票:4回答:1

今天我从w3schools尝试了node.js mysql片段:

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");
  });
});

我想学习如何处理mysql错误,因为我的应用程序需要mysql。但在我开始app.js文件this error showed up之后。为什么我不能抛出错误?

javascript mysql node.js server-side
1个回答
2
投票

我想学习如何处理mysql错误,因为我的应用程序需要mysql。

MySQLJS Error handling

要捕获throw的错误,请尝试使用以下代码段:

con.on('error', function(err) {
  console.log("[mysql error]",err);
});
© www.soinside.com 2019 - 2024. All rights reserved.