【问题标题】:i have created mysql connection in nodejs it is showing database connected but showing an error as undefined database我在nodejs中创建了mysql连接,它显示数据库已连接,但显示错误为未定义数据库
【发布时间】:2018-05-21 06:08:18
【问题描述】:

var mysql = require('mysql');

var 连接 = mysql.createConnection({

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

});

connection.connect(函数(错误){

if(!err) {

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

});

this is the error i got

【问题讨论】:

  • 数据库好像没有声明,你确定这个变量存在吗?
  • console.log("database is connected"+database); 中的变量database 未定义,你的作用域中没有这样的变量,但似乎连接成功

标签: mysql node.js database-connection


【解决方案1】:

将您的连接函数更改为如下所示:(将函数替换为粗箭头函数“=>”)

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?

【讨论】:

  • 你试过删除邮件吗? (console.log("数据库已连接"+数据库))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-26
相关资源
最近更新 更多