无法在nodeJS中validation用户:TypeError:无法使用“in”运算符在fs中search“用户名”

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

这里是htmllogin表单:

<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> 

当用户按下submitbutton时,我的控制台上显示如下:

 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)。 我也尝试将策略属性添加到LocalPassport 。 这些错误是由gnerkus指出的,但是当我得到同样的错误时,他们没能解决这个问题。

       

网上收集的解决方案 "无法在nodeJS中validation用户:TypeError:无法使用“in”运算符在fs中search“用户名”"

发生此错误的原因是User对象未被实例化。 这是因为模型是无效的。 应该创build一个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对象中获得的。 您需要replace这一行:

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

用这一行:

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