Node.js MySQL模块-抛出err;//抛出非MySQL错误; [英] Node.js MySQL module - throw err; // Rethrow non-MySQL errors;
本文介绍了Node.js MySQL模块-抛出err;//抛出非MySQL错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天我尝试了来自w3schools的node.js mysql代码段:
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");
});
});
我想学习如何处理mysql错误,因为我的应用需要mysql.但是在我启动app.js文件后,出现了以下错误:
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?
推荐答案
我想学习如何处理mysql错误,因为我的应用要求 mysql.
I wanted to learn how to handle mysql errors, because my app require mysql.
要捕获您throw
的错误,请尝试使用以下代码段:
To catch the errors you throw
, try with the following snippet :
con.on('error', function(err) {
console.log("[mysql error]",err);
});
这篇关于Node.js MySQL模块-抛出err;//抛出非MySQL错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文