charly rl
charly rl

Reputation: 885

Sequelize Authentication cannot complete

I'm starting out with node.js and sequelize and I get the following error:

/home/cbaket/test/test.js:9
    .complete(function(err) {
     ^
TypeError: undefined is not a function
    at Object.<anonymous> (/home/cbaket/test/test.js:9:6)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

My file: test.js is:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('apidb', 'apidb', 'apidb', {
    dialect: "mysql", // or 'sqlite', mysql', 'mariadb'
    port:    3306 // or 5432 (for postgres)
});

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

I'm following one of the early tutorials on the Sequelieze website.

I installed the latest sequelize and mysql with the following command.

$ npm install mysql  
$ npm install sequelize

I have tried a lot of similar examples and always get the same error. The libraries are working because other examples work fine, i could create tables in the database and get data from it but the authenticate function always fails.

Thanks! ;)

Upvotes: 7

Views: 17570

Answers (5)

Rama
Rama

Reputation: 195

Uninstall the sequelize module which is installed in your system and install this version using

npm install [email protected]

Check the models folder and make sure it contains proper files

Upvotes: 0

Alex Koutmos
Alex Koutmos

Reputation: 326

Authenticate returns a Promise in the later versions of sequelize: https://github.com/sequelize/sequelize/blob/2.0/lib/sequelize.js#L939

Read the bluebird documentation here as it is quite useful: https://github.com/petkaantonov/bluebird/blob/master/API.md

Something like this should work (make sure you check for connection errors...this is just a simple example):

var Sequelize = require('sequelize');
var sql = new Sequelize('DB', 'UNAME', 'PASS', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});

var test = sql.authenticate()
    .then(function () {
        console.log("CONNECTED! ");
    })
    .catch(function (err) {
        console.log("SOMETHING DONE GOOFED");
    })
    .done();

Upvotes: 21

Sathish
Sathish

Reputation: 2180

Hi sequelize changed their auth scheme in latest release.. Please revert back to older version and try it

Upvotes: 4

charly rl
charly rl

Reputation: 885

you were right!! ;) I just remove sequelize and then reinstall an old veriosn:

npm install [email protected]

and it works!!

I didn't found any info about that... Shouldn't they warn users from deprecated or modified functions ?¿?...

THANKS!! ;)

Upvotes: 1

JoshWillik
JoshWillik

Reputation: 2645

I suspect you're looking for

Sequelize.authenticate().complete(...)

instead of

Sequelize.authenticate.complete(...)

Sequelize.authenticate is a function that returns a promise. It's not an object with a .complete() method. If you don't execute it, no promise will be returned. If no promise is returned, you can't run .complete() on the promise.

Hope this helps you out.

Here is some documentation on Promises from the Mozilla Development Network.

Upvotes: 0

Related Questions