cphill
cphill

Reputation: 5924

NodeJS - Connecting to a local Mysql Database

I am trying to create a mysql database connection for my node app and ran sequelize.authenticate().then(function(errors) { console.log(errors) }); to test if the connection worked. The response that is logged to my console is Executing (default): SELECT 1+1 AS result undefined The undefined portion makes me think that the connection either didn't work or that there isn't any database found. I can't seem to figure that out. I thought by creating a database through Sequel Pro and connecting to localhost via the Socket, I can use the same credentials for connecting with my Node app. Do I need to create a file within my app for the database and not use the Sequel Pro database?

Controller file (where the connection is created):

var Sequelize = require('sequelize');
var sequelize = new Sequelize('synotate', 'root', '', {
    host:'localhost',
    port:'3306',
    dialect: 'mysql'
});

sequelize.authenticate().then(function(errors) { console.log(errors) });

var db = {}

db.Annotation = sequelize.import(__dirname + "/ann-model");

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

ann-model.js (where my table is being defined):

module.exports = function(sequelize, DataTypes) {

var Ann = sequelize.define('annotations', {
    ann_id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    ann_date: DataTypes.DATE,

}, {
    freezeTableName: true
});
    return Ann;
}

database for localhost

Upvotes: 2

Views: 953

Answers (1)

Anatoly Ruchka
Anatoly Ruchka

Reputation: 543

Try this one, Executing (default): SELECT 1+1 AS result means that everything okay

 sequelize
        .authenticate()
        .then(function(err) {
            if (!!err) {
                console.log('Unable to connect to the database:', err)
            } else {
                console.log('Connection has been established successfully.')
            }
        });

But i didn't know where you get undefined

Upvotes: 2

Related Questions