gpt4 book ai didi

node.js - Passport 注销()错误: Cast to ObjectId failed for value "logout" at path "_id" for model "somemodel(not the user model)"

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:47 25 4
gpt4 key购买 nike

我是一名业余编码员,通常可以通过大量搜索来解决错误,但这次我无法正确解决。

当我点击注销路由时,它会抛出错误:对于模型“Spot”的路径“_id”处的值“logout”,转换到 ObjectId 失败

我尝试了 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);

谢谢

最佳答案

因为 app.get("/:id", ...) 写在 app.get("/logout", ...) 之前代码,我猜当您获得 /logout 时,/:id 的请求处理程序将被调用。然后,req.params.id 变为 "logout" 并且由 Spot.findById() 抛出错误。

尝试在 app.get("/:id", ...) 之前编写 app.get("/logout", ...) 怎么样? ?

关于node.js - Passport 注销()错误: Cast to ObjectId failed for value "logout" at path "_id" for model "somemodel(not the user model)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52946792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com