我有一个节点js App,当我尝试使用本地数据库(mysql)运行节点js时,我使用mamp并显示错误Localhost refuse to connect
并显示日志TypeError: Cannot read property 'query' of undefined
。但是当我尝试在服务器中使用db运行节点js时没问题。
app.js
var mysql = require("mysql");
function Connection() {
this.pool = null;
var konek = {
host: 'localhost',
user: 'root',
password: 'root',
database: 'wmc_wahana'
};
this.init = function() {
this.pool = mysql.createPool(konek);
}
this.acquire = function(callback) {
this.init();
this.pool.getConnection(function(err, connection) {
callback(err, connection);
});
};
}
module.exports = new Connection();
server.js
var app = require('./app');
var http = require('http').Server(app);
var port = '8080';
http.listen(port, function() {
console.log('Server listening on port ' + port);
});
谢谢
答案 0 :(得分:0)
请将“localhost”更改为“127.0.0.1”