Try to search your question here, if you can't find : Ask Any Question Now ?

Node.js mongoose User.register makes the page keep pending

HomeCategory: stackoverflowNode.js mongoose User.register makes the page keep pending
Avatarjulie asked 5 months ago

This is my app.js

var express               = require("express")
var User                  = require("./models/user");
var bodyParser            = require("body-parser");
var mongoose              = require("mongoose");
var passport              = require("passport");
var passportStrategy      = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");



//mongo db
mongoose.connect("mongodb://localhost/auth_demo_app");



var app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true }));
app.use(require("express-session")({
  secret: "Rusty",
  resave: false,
  saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());


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

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

//Auth Routhes
//show the form
app.get("/register", function(req,res) {
  res.render("register");
});


app.post("/register", function(req,res) {
  req.body.username
  req.body.password
  User.register(new User({username: req.body.username}), req.body.password, function(err, user) {
    if(err) {
      console.log(err);
      return res.render('register');
    }
    passport.authenticate("local")(req, res, function(){
        res.redirect("/secret");
    });
  });
});



//this is the locall host
app.listen(3002, function(){
  console.log("Server on port 3002")
});

This is the user model user.js

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

can someone help me spot the problem why my page is keep pending none stop
cause I don’t find any problem with the code just the row with User.register
do the problem.
it’s looks like it can’t go inside this method but I still don’t sure why
it will be great if you spot the error.

cause every time i look on stackoverflow I see people forget to put the user model like I did.
but this is not the case

1 Answers
Best Answer
AvatarArben answered 5 months ago
Your Answer

4 + 9 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com