我有一个node js应用程序,当我尝试使用本地数据库(mysql)运行node js时,我使用mamp并显示错误 Localhost refuse to connect
和日志显示 TypeError: Cannot read property 'query' of undefined
. 但是,当我尝试在服务器中使用db运行节点js时,就可以了。
应用程序.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();
服务器.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);
});
谢谢您
1条答案
按热度按时间bfhwhh0e1#
请将“localhost”改为“127.0.0.1”