我使用passport.js对Express和Sequelize进行了以下身份验证策略:
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);
})
);
};
问题在于,当我执行代码时,它无法插入到db中,并出现以下错误:
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" = 'user@email.com') 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)
我知道这是在用户创建期间,因为如果我使用相同的电子邮件地址来解决错误,那么一切都有效。但是如果它开始创建一个用户就会出错,并且网页会停止。
更新:如果我只使用.find
运行它:
Executing (default): SELECT "id", "email", "password", "lastLogin", "createdAt", "updatedAt", "deletedAt" FROM "Users" AS "User" WHERE ("User"."deletedAt" IS NULL AND "User"."email" = 'user@email.com') 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)
更新:已移除.spread
:
Executing (default): SELECT "id", "email", "password", "lastLogin", "createdAt", "updatedAt", "deletedAt" FROM "Users" AS "User" WHERE ("User"."deletedAt" IS NULL AND "User"."email" = 'user@email.com') 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)
答案 0 :(得分:1)
sequelize模型不是构造函数(即你不能调用new
)。
http://docs.sequelizejs.com/en/latest/api/model/
将其用作工厂,方法是调用build
,返回一个未保存的新实例,或调用create
,返回一个使用已保存实例解析的承诺