Ratri
Ratri

Reputation: 337

Node js can't connect to local database

I have a node js App, and when i try to run the node js using local database (mysql) and i use mamp and the error shown Localhost refuse to connect and log shown TypeError: Cannot read property 'query' of undefined. But when i try running the node js using db in server is fine.

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);
});

thank you

Upvotes: 1

Views: 1580

Answers (1)

Tohir
Tohir

Reputation: 142

pls change "localhost" to "127.0.0.1"

Upvotes: 1

Related Questions