护照注销()错误:对于模型“somemodel(不是用户模型)”的路径“_id”处的值“注销”,转换为 ObjectId 失败
Posted
技术标签:
【中文标题】护照注销()错误:对于模型“somemodel(不是用户模型)”的路径“_id”处的值“注销”,转换为 ObjectId 失败【英文标题】:passport logout() error: Cast to ObjectId failed for value "logout" at path "_id" for model "somemodel(not the user model)" 【发布时间】:2019-03-27 13:24:07 【问题描述】:我是一个业余编码爱好者,我通常可以通过大量搜索来解决错误,但我无法正确解决这个问题。
当我点击注销路线时,它会引发错误:Cast to ObjectId failed for value "logout" at path "_id" for model "Spot"
我尝试了 mongoose 版本 4.7.2,它不工作。我无法想象为什么它将我的注销路线与现场模型相关联。
我的 app.js
var express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
passportFacebook = require("passport-facebook").Strategy,
User = require("./models/user.js"),
Spot = require("./models/spot.js");
mongoose.connect("mongodb://localhost/biketrialspots", useNewUrlParser: true );
var app = express();
app.set("view engine","ejs");
app.use(bodyParser.urlencoded(extended:true));
app.use(express.static('public'));
app.use(require("express-session")(
secret: "some secret",
resave: false,
saveUninitialized: false
));
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next)
res.locals.currentUser = req.user;
next();
);
passport.use(new passportFacebook(
clientID: "some id",
clientSecret: "some secret",
callbackURL: "somewebsite/auth/facebook/callback",
profileFields: ['id', 'displayName', 'picture.type(large)']
, function(accessToken, refreshToken, profile, done)
User.findOrCreate(profile, function(err, user)
if (err)
return done(err);
done(null, user);
);
));
passport.serializeUser(function(user, done)
done(null, user._id);
);
passport.deserializeUser(function(id, done)
User.findById(id, function(err, user)
done(err, user);
);
);
app.get("/", function(req, res)
Spot.find(, function(err, spots)
if(err)
console.log(err);
else
res.render("index", spots:spots);
);
);
app.get("/new", ensureAuthenticated, function(req, res)
res.render("new");
);
app.post("/", function(req, res)
Spot.create(req.body.spot, function(err, result)
if(err)
console.log(err);
else
res.redirect("/");
);
);
app.get("/:id", function(req, res)
Spot.findById(req.params.id, function(err, spot)
if(err)
console.log(err);
else
res.render("spot", spot: spot);
);
);
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
app.get('/auth/facebook', passport.authenticate('facebook'));
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', successRedirect: '/',
failureRedirect: '/login' ));
app.get("/logout", function(req, res)
req.user.logout();
res.redirect('/');
);
function ensureAuthenticated(req, res, next)
if (req.isAuthenticated()) return next();
return res.redirect('/');
app.listen(process.env.PORT, process.env.IP, function(req, res)
console.log("APP HAS STARTED!!!!!");
);
用户模型
var mongoose = require("mongoose");
var userSchema = new mongoose.Schema(
facebookID:Number,
username:String,
photo:String
);
userSchema.statics.findOrCreate = function findOrCreate(profile, cb)
var userObj = new this();
this.findOne(facebookID : profile.id,function(err,result)
if(!result)
userObj.facebookID = profile.id;
userObj.username = profile.displayName;
userObj.photo = profile.photos[0].value;
userObj.save(cb);
else
cb(err,result);
);
;
module.exports = mongoose.model("User", userSchema);
谢谢
【问题讨论】:
【参考方案1】:因为app.get("/:id", ...)
在你的代码中写在app.get("/logout", ...)
之前,我猜/:id
的请求处理程序会在你得到/logout
时被调用。然后,req.params.id
变为"logout"
并由Spot.findById()
抛出错误。
尝试在app.get("/:id", ...)
之前写app.get("/logout", ...)
怎么样?
【讨论】:
以上是关于护照注销()错误:对于模型“somemodel(不是用户模型)”的路径“_id”处的值“注销”,转换为 ObjectId 失败的主要内容,如果未能解决你的问题,请参考以下文章
req.session.destroy 和护照注销不会破坏客户端的 cookie