【问题标题】:PASSPORT AUTHENTICATION USING EXPRESS NOT RESPONDING使用快速不响应的护照认证
【发布时间】:2019-10-03 16:56:17
【问题描述】:

我正在尝试使用 nodejs 和 mongodb 使用护照身份验证, 下面是代码,我试过了,没有得到任何回应。 它甚至没有进入 findOrCreate 方法。

我正在使用nodejs v: 10.16.3npm v: 6.9.0

mongodb version:4.2.0,操作系统为MAC OS Mojave

这里是依赖列表-

"dependencies": {
    "body-parser": "^1.19.0",
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.4.4",
    "express": "^4.17.1",
    "express-session": "^1.16.2",
    "flash": "^1.1.0",
    "http": "0.0.0",
    "mongo": "^0.1.0",
    "mongodb": "^3.3.2",
    "mongoose": "^4.3.5",
    "mysql": "^2.17.1",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0"
  }

护照代码如下-

 app.post("/signup",(req,res)=>{
    console.log("Inside Signup",req.body);
    res.send(req.body);
    var emails=req.body.Email;
    console.log(emails);
    var names=req.body.Name;
    console.log(names);
    var passwords=req.body.Password;
   console.log(passwords);
    si(names,emails,passwords);
  });
 function si(username,email,password){
      passport.use('signup', new LocalStrategy({
        passReqToCallback : true
      },
      function(req, username, password, done) {
        findOrCreateUser = function(){
          // find a user in Mongo with provided username
    console.log("Step1 success");
          console.log(username);
          console.log(email);
          console.log(password);
          var dbo = db.db("record");
          dbo.collection("records").findOne({'Username':username}, function(err, result) {
            if (err){
              console.log('Error in SignUp: '+err);
              return done(err);
            }
            // already exists
            if (result) {
              console.log('User already exists');
              return done(null, false, 
                 req.flash('message','User Already Exists'));
            } else {
              var newUser = new User();
              newUser.Username = username;
              newUser.Password = password;
              newUser.Email = email;
              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);
      }))
};

任何人都可以建议,我怎样才能使它工作?

【问题讨论】:

  • 哪里没有响应?什么错误/日志以及在哪里?
  • 护照根本没有执行。就连console.log("step1 Success") 也不安慰。

标签: node.js mongodb passport.js


【解决方案1】:

尝试通过将对象传递给构造函数来创建用户,而不是单独更改属性。

newUser = new User({ username: username, email: emal, password: password });

【讨论】:

  • 我同意这一点,我认为与其调用new User(),不如先聚合newUser var 中的所有属性,然后调用new User(newUser) 并将newUser 对象传递给它然后保存。
  • 另外,在原始实现中属性的首字母为大写是否正确?
  • 现在再看看这个问题,据说执行甚至没有达到那个功能所以这可能是另一个错误而不是描述的那个。
  • 我试图实现你所说的,但它也不起作用。我现在正在上传整个节点端代码,以便它可以让你更清楚。等待快速响应。
  • 像这样使用 findOrCreateUser 似乎很复杂,也许可以尝试在回调之外声明该函数,然后在需要的任何地方调用它。
猜你喜欢
  • 2015-01-19
  • 2017-10-14
  • 2023-03-14
  • 2016-10-27
  • 2017-06-09
  • 2017-10-05
  • 1970-01-01
  • 2018-11-26
  • 2015-06-01
相关资源
最近更新 更多