user14118101
user14118101

Reputation:

[MissingUsernameError]: No username was given Mongoose

I have had issues registering a user onto my mongo database. When I have the code like below, it comes up with the error of [MissingUsernameError]: No username was given

I have tried to replace the {email: req.body.email} with {username: req.body.email} however this sends back a "Bad Request". Thanks in advance

   app.post("/register", function(req, res){
    
      User.register({email: req.body.email}, req.body.password, function(err, user){
        if (err) {
          console.log(err);
          res.redirect("/register");
        } else {
          passport.authenticate("local")(req, res, function(){
            res.redirect("/");
          });
        }
      });
    
    });

Upvotes: 0

Views: 227

Answers (2)

user14118101
user14118101

Reputation:

Update: I went through the documentation and added a option that made it so that the username wasn't requred and the email field was!

userSchema.plugin(passportLocalMongoose, {usernameField: 'email'})

Upvotes: 0

Tina
Tina

Reputation: 544

try this one:

const newUser = new User({
    email: req.body.email,
    password: req.body.password,
  });
  newUser
    .save()
    .then((result) => {
      //console.log("Printing Result-->>", result);
      res.status(httpStatusCode.CREATED).json({
        success: true,
        message: "Congratulations!! Successfully Created Account!!",
      });
    })
    .catch((error) => {
      // console.log("Error-->>", error);
      res.status(httpStatusCode.INTERNAL_SERVER_ERROR).json({
        success: false,
        error: "Failed To Create New Account!! Try Again!!",
      });
    });

Upvotes: 1

Related Questions