无法在 nodeJS 中对用户进行身份验证:TypeError:无法使用“in”运算符在 fs 中搜索“用户名”

Posted

技术标签:

【中文标题】无法在 nodeJS 中对用户进行身份验证:TypeError:无法使用“in”运算符在 fs 中搜索“用户名”【英文标题】:Unable to authenticate user in nodeJS:TypeError: Cannot use 'in' operator to search for 'username' in fs 【发布时间】:2016-05-07 19:24:41 【问题描述】:

我有一个简单的表单来注册/验证用户。我能够成功注册用户,但无法进行身份验证。我试图研究这个问题,但我没有发现任何可能有帮助的东西。

这里是html登录表单:

<form method="post" action="/login">
   <input type="text" name="username" placeholder="username">
   <input type="password" name="password" placeholder="password">
   <input type="submit" name="submit" value="submit">


</form>

当用户按下submit 按钮时,我的控制台上会显示以下内容:

TypeError: Cannot use 'in' operator to search for 'username' in fs
    at model.Document.$__buildDoc (/Users/userA/Desktop/loginapp/node_modules/mongoose/lib/document.js:207:39)
    at model.Document (/Users/userA/Desktop/loginapp/node_modules/mongoose/lib/document.js:61:20)
    at model.Model (/Users/userA/Desktop/loginapp/node_modules/mongoose/lib/model.js:45:12)
    at new model (/Users/userA/Desktop/loginapp/node_modules/mongoose/lib/model.js:2957:11)
    at Strategy.model [as _verify] (/Users/userA/Desktop/loginapp/node_modules/mongoose/lib/model.js:2956:14)
    at Strategy.authenticate (/Users/userA/Desktop/loginapp/node_modules/passport-local/lib/strategy.js:90:12)
    at attempt (/Users/userA/Desktop/loginapp/node_modules/passport/lib/middleware/authenticate.js:348:16)
    at authenticate (/Users/userA/Desktop/loginapp/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at Layer.handle [as handle_request] (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/layer.js:95:5)
    at /Users/userA/Desktop/loginapp/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/userA/Desktop/loginapp/node_modules/express/lib/router/index.js:271:10)
    at SessionStrategy.strategy.pass (/Users/userA/Desktop/loginapp/node_modules/passport/lib/middleware/authenticate.js:325:9)

app.js:

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var session = require('express-session');
var passport = require('passport');
var bodyParser = require('body-parser');
var User = require('./model/user.js');
var LocalStrategy = require('passport-local').Strategy;
var passportLocalMongoose = require('passport-local-mongoose');
var ejs = require('ejs');
var dbURL = 'mongodb://Admin:123@ds051655.mongolab.com:51655/yelpcamp';


mongoose.connect(dbURL, function(err) 
    err ? console.log(err) : console.log('successfully connected to db!');
);


app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded(extended:true));


app.use(session(
    secret:"sfsdfsdfsd",
    resave: false,
    saveUninitialized: false
));

app.use(passport.initialize());
app.use(passport.session());

passport.use(new LocalStrategy(User, User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.post("/login", passport.authenticate("local", 

    successRedirect: "/secret",
    failureRedirect: "/login"


), function(req,res) 
);

user.js:

var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');

var userSchema = new mongoose.Schema(
    username: String,
    password: String,
    email: String,
    pictures: [type:mongoose.Schema.Types.ObjectId, ref: "Pictures"],
    comments: [type: mongoose.Schema.Types.ObjectId, ref: "Comments" ]  
);

userSchema.plugin(passportLocalMongoose);
var User = mongoose.model("User", userSchema);

module.exports = User;

package.json:

 "dependencies": 
    "body-parser": "^1.14.2",
    "connect-mongo": "^1.1.0",
    "ejs": "^2.4.1",
    "express": "^4.13.4",
    "express-session": "^1.13.0",
    "mongoose": "^4.3.7",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^4.0.0"
  

update1:为了解决这个问题,我尝试在用户类中实例化 User 对象(mongoose.Schema 已更改为新的 mongoose.Schema)。我还尝试将 Strategy 属性添加到 LocalPassport。 gnerkus 指出了这些错误,但他们未能解决问题,因为我遇到了同样的错误。

【问题讨论】:

【参考方案1】:

发生错误是因为User 对象没有被实例化。这是因为模型无效。应将 Mongoose 模式创建为 mongoose.Schema 构造函数的实例。您需要重构模型以使用 Schema 实例:

// Create instance of 'Schema' constructor.
var userSchema = new mongoose.Schema(
    username: String,
    password: String,
    email: String,
    pictures: [type:mongoose.Schema.Types.ObjectId, ref: "Pictures"],
    comments: [type: mongoose.Schema.Types.ObjectId, ref: "Comments" ]  
);

另外,LocalStrategy 构造函数是从passport-localStrategy 对象中获得的。您需要替换此行:

var LocalStrategy = require('passport-local');

用这一行:

var LocalStrategy = require('passport-local').Strategy;

【讨论】:

感谢您的回复。我试过这段代码,但同样的问题仍然存在。 我已经找到了解决方案。请查看更新后的答案。 我进行了建议的更改(基本上是在 mongoose.Schema 之前添加新的),但是没有用;同样的问题仍然存在。但是,感谢您指出此错误(:

以上是关于无法在 nodeJS 中对用户进行身份验证:TypeError:无法使用“in”运算符在 fs 中搜索“用户名”的主要内容,如果未能解决你的问题,请参考以下文章

在没有管理员登录的情况下使用nodejs ldapauth进行身份验证?

如果用户已经通过登录页面或 auth0 进行了身份验证,我们是不是需要在 websocket 连接中对用户进行身份验证?

如何在 shoudPerformSegueWithIdentifier 和 Firebase 中对用户进行身份验证?

在 SPA 中对访客/匿名用户进行身份验证

无法在 python 中对 Google Cloud Storage 客户端进行身份验证

如何在 wordpress 中对用户进行身份验证?