502 - 尝试通过Passport JS注册用户时出现错误网关

时间:2017-07-06 16:11:42

标签: node.js mongodb express passport.js passport-local

所以我使用Passport Local构建本地注册和身份验证系统。 我认为注册有效,因为它显示在终端:

Mongoose: users.findOne({ username: 'user6' }, { fields: undefined })
User already exists with username: user6
message User Already Exists
Mongoose: users.findOne({ username: 'user7' }, { fields: undefined })
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
Mongoose: users.insert({ info: 'hujk,trfyulryflrfyuk,yhj', name: 'User Seven', email: 'user7@gmail.com', password: '$2a$10$d1fCoF7r3lRsyzbAzZlw0e74rqPhXLL7LatQaNSIi4UIVtPhXRGH.', username: 'user7', _id: ObjectId("59567dd1004be60ca0bf44a2"), isAdmin: false, __v: 0 })
User Registration succesful
Mongoose: users.findOne({ _id: ObjectId("59567dd1004be60ca0bf44a2") }, { fields: undefined })

问题在于,此注册最终导致502 - Bad Gateway错误而不是主页。此外,当我去MongoDB并搜索所有用户时,不会出现user6和user7。我检查了,网址仍然存在。 这是本地注册码:

passport.use('local-signup', new LocalStrategy({
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, username, password, email, name, info, done) {

        function findOrCreateUser(){
            // find a user in Mongo with provided username
            User.findOne({ 'username' :  username }, function(err, user) {
                // In case of any error, return using the done method
                if (err){
                    console.log('Error in SignUp: '+err);
                    return done(err);
                }
                // already exists
                if (user) {
                    console.log('User already exists with username: '+username);
                    return done(null, false, console.log('message','User Already Exists'));
                } else {
                    // if there is no user with that email
                    // create the user
                    var newUser = new User();

                    // set the user's local credentials
                    newUser.username = username;
                    newUser.password = createHash(password);
                    newUser.email = email;
                    newUser.name = name;
                    newUser.info = info;
                    // save the user
                    newUser.save(function(err) {
                        if (err){
                            console.log('Error in Saving user: '+err);  
                            throw err;  
                        }
                        console.log('User Registration succesful');    
                        return done(null, newUser);
                    });
                }
            });
        }
        // Delay the execution of findOrCreateUser and execute the method
        // in the next tick of the event loop
        process.nextTick(findOrCreateUser);
    }));

有没有人有任何想法?我无法在任何地方找到它。

0 个答案:

没有答案