Reputation: 2110
I have the following authentication strategy for Express and Sequelize using passport.js:
var LocalStrategy = require('passport-local').Strategy;
var User = require('../../models').User;
module.exports = function (passport) {
passport.use('register', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function (req, email, password, done) {
//var findOrCreateUser = function () {
// find a user in Mongo with provided username
User.findOrCreate({where: {email: email}}).spread(
function (user, created) {
// In case of any error, return using the done method
//if (err) {
// console.log('Error in SignUp: ' + err);
// return done(err);
//}
if (email !== req.param('email-confirm')) {
console.log('Registration: Email address do not match');
return done(null, false, {message: 'Email addresses do not match'});
}
// already exists
if (user) {
console.log('User ' + email + ' is already registered.');
return done(null, false, {'message': 'The email ' + email + ' is already registered.'});
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.email = email;
newUser.password = password;
// save the user
newUser.save(function (err) {
if (err) {
console.log('Error in Saving user: ' + err);
throw err;
}
console.log('New user registration', newUser);
return done(null, newUser);
});
}
});
//};
//// Delay the execution of findOrCreateUser and execute the method
//// in the next tick of the event loop
//process.nextTick(findOrCreateUser);
})
);
};
The problem is that when I execute the code it fails to insert into the db with the following error:
Executing (fa4929e4-6b35-40ce-9e02-daf80af507e1): START TRANSACTION;
Executing (fa4929e4-6b35-40ce-9e02-daf80af507e1): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (fa4929e4-6b35-40ce-9e02-daf80af507e1): SET autocommit = 1;
Executing (fa4929e4-6b35-40ce-9e02-daf80af507e1): SELECT "id", "email", "password", "lastLogin", "createdAt", "updatedAt", "deletedAt" FROM "Users" AS "User" WHERE ("User"."deletedAt" IS NULL AND "User"."email" = '[email protected]') LIMIT 1;
Executing (fa4929e4-6b35-40ce-9e02-daf80af507e1): COMMIT;
Unhandled rejection TypeError: Object [object SequelizeInstance:User] has no method 'isModified'
at sequelize.define.instanceMethods.save (/home/otis/Developer/Project/models/users.js:24:26)
at Model.create (/home/otis/Developer/Project/node_modules/sequelize/lib/model.js:1735:6)
at Object.<anonymous> (/home/otis/Developer/Project/node_modules/sequelize/lib/model.js:1837:17)
at Object.tryCatcher (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:503:31)
at Promise._settlePromiseAt (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:577:18)
at Async._drainQueue (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:128:12)
at Async._drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:133:10)
at Async.drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
at process._tickCallback (node.js:415:13)
I know it is during the user creation because if I get it to error with the same email address that all works. But if it starts to create a user it errors out, and the web page stalls.
Update: If I run it with just .find
:
Executing (default): SELECT "id", "email", "password", "lastLogin", "createdAt", "updatedAt", "deletedAt" FROM "Users" AS "User" WHERE ("User"."deletedAt" IS NULL AND "User"."email" = '[email protected]') LIMIT 1;
Unhandled rejection TypeError: expecting an array, a promise or a thenable
See http://goo.gl/s8MMhc
at PromiseArray.init [as _init] (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise_array.js:42:27)
at Promise._settlePromiseAt (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:575:21)
at Promise._settlePromises (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:693:14)
at Async._drainQueue (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:133:10)
at Async.drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
at process._tickCallback (node.js:415:13)
Update: removed .spread
:
Executing (default): SELECT "id", "email", "password", "lastLogin", "createdAt", "updatedAt", "deletedAt" FROM "Users" AS "User" WHERE ("User"."deletedAt" IS NULL AND "User"."email" = '[email protected]') LIMIT 1;
express deprecated req.param(name): Use req.params, req.body, or req.query instead middlewares/authentication/registration-strategy.js:21:39
Unhandled rejection TypeError: object is not a function
at null.<anonymous> (/home/otis/Developer/Project/middlewares/authentication/registration-strategy.js:35:39)
at tryCatcher (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:503:31)
at Promise._settlePromiseAt (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:577:18)
at Promise._settlePromises (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:693:14)
at Async._drainQueue (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:133:10)
at Async.drainQueues (/home/otis/Developer/Project/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
at process._tickCallback (node.js:415:13)
Upvotes: 3
Views: 1658
Reputation: 28778
The sequelize model is not a constructor (i.e. you can't call new
).
http://docs.sequelizejs.com/en/latest/api/model/
Use it as a factory, either by calling build
, which returns a new, unsaved instance, or calling create
which returns a promise that resolves with a saved instance
Upvotes: 1