我在nodejs中创建了mysql连接,它显示数据库已连接,但显示错误为未定义的数据库

时间:2017-12-07 07:36:49

标签: mysql node.js database-connection

var mysql = require(' mysql');

var connection = mysql.createConnection({

host : 'localhost',
user : 'root',
port : '3306',
password : 'root',
database : 'test'

});

connection.connect(函数(ERR){

if(!err) {

    console.log("database is connected"+database);
} else {
    console.log("database is not connected"+err.message);
}

});

this is the error i got

1 个答案:

答案 0 :(得分:0)

将连接功能更改为如下所示:(用胖箭头功能替换功能" =>")

connection.connect((err) => {
    if (!err) {
        console.log("database is connected"+database);
    } else {
        console.log("database is not connected"+err.message);
    }
});

以下链接解释了原因:

What is the difference between () => {} and function() {} in react-native javascript?