saintedlama / passport-local-mongoose

Passport-Local Mongoose is a Mongoose plugin that simplifies building username and password login with Passport
MIT License
1.17k stars 295 forks source link

Error coming inside of index.js in passport-local-mongoose. #272

Closed VisheshPandita closed 3 years ago

VisheshPandita commented 6 years ago

This is the error which is coming, its syntax error, but when cleared leads to chain of errors.

/home/vishesh/Desktop/Programming/web dev/yelpcamp/v5/node_modules/passport-local-mongoose/index.js:125 .then(({ user }) => { ^ SyntaxError: Unexpected token { at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object. (/home/vishesh/Desktop/Programming/web dev/yelpcamp/v5/models/user.js:2:29) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object. (/home/vishesh/Desktop/Programming/web dev/yelpcamp/v5/app.js:9:18) at Module._compile (module.js:410:26)

my Model is: `var mongoose = require("mongoose"); var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({ username: String, password: String });

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);`

And main code is: `var express = require("express"), app = express(), bodyParser = require("body-parser"), mongoose = require("mongoose"), passport = require("passport"), LocalStrategy = require("passport-local"), Campground = require("./models/campground"), Comment = require("./models/comment"), User = require("./models/user"), seedDB = require("./seeds");

mongoose.connect("mongodb://localhost/yelp_camp"); app.set("view engine", "ejs"); app.use(express.static(__dirname + "/public")); app.use(bodyParser.urlencoded({extended: true}));

// seedDB();

// PASSPORT CONFIGURATION app.use(require("express-session")({ secret: "No country for old men", resave: false, saveUninitialized: false })) app.use(passport.initialize()); app.use(passport.session()); passport.use(new LocalStrategy(User.authenticate())); passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser());

app.get("/", function(req, res){ res.render("landing"); });

app.get("/campgrounds", function(req, res){ // Get all the capmgrounds form DB Campground.find({}, function(err, allCampgrounds){ if(err){ console.log(err); } else{ res.render("campgrounds/index", {campgrounds: allCampgrounds}); } });

});

app.post("/campgrounds", function(req, res){ var name = req.body.name; var image = req.body.image; var desc = req.body.description; var newCampground = {name: name, image: image, description: desc};

//Create new Campground and save it to db
Campground.create(newCampground, function(err, newlyCreated){
    if(err){
        console.log(err);
    } else{
        res.redirect('/campgrounds');
    }
});

});

app.get("/campgrounds/new", function(req, res){ res.render("campgrounds/new"); });

//SHOW- show us more info about one campground app.get("/campgrounds/:id", function(req, res){

Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
    if(err){
        console.log(err);
    } else{
        res.render("campgrounds/show", {campground: foundCampground});
    }
});

});

//============================ // COMMENTS ROUTES //============================

app.get("/campgrounds/:id/comments/new", function(req, res){ Campground.findById(req.params.id, function(err, campground){ if (err) { console.log(err); } else{ res.render("comments/new",{campground: campground}); }

})

});

app.post("/campgrounds/:id/comments", function(req, res){ Campground.findById(req.params.id, function(err, campground){ if (err) { console.log(err); } else{ Comment.create(req.body.comment, function(err, comment){ campground.comments.push(comment); campground.save();

            res.redirect("/campgrounds/"+ campground._id);
        })
    }
});

});

// =========== // AUTH ROUTES // ===========

// show register form app.get("/register", function(req, res){ res.render("register"); }); // handel sign up logic app.post("/register", function(req, res){ var newUser = new User({username: req.body.username}); User.register(newUser, req.body.password, function(err, user){ if (err) { console.log(err); return res.render("register"); } passport.authenticate("local")(req, res, function(){ res.redirect("/campgrounds"); }); }) });

// show Log in form app.get("/login", function(req, res){ res.render("login"); }); // handel log in form app.post("/login",passport.authenticate("local", { successRedirect: "/campgrounds", failureRedirect: "/login"

}), function(req, res){});

app.listen(3000, function(){ console.log("YelpCamp server is onn!"); });`

rRajivramachandran commented 3 years ago

Any updates?? I too encountered the same issue

VisheshPandita commented 3 years ago

It was a syntax error. Check the code again thoroughly.