View the step-by-step solution to:

How to avoid the duplicate user name in my code?

How to avoid the duplicate user name in my code?

p.s( I use the mongodb to save data)

Here is my code. Thank you a lot.




const User = require('../models/user');

module.exports = {


 // show the home page

 showForm: (req, res) => {

   res.render('pages/register');

 },

 signUp: (req, res) => {

     console.log(req.body);

     const user = req.body;

     var newUser = new User(user);

     console.log(newUser);

       newUser.save();

   res.render('pages/success');

 },


   showLogin: (req, res) => {

   res.render('pages/login');

 },


 login: (req, res) => {

     console.log(req.body);

     const user = req.body;

     var query = { "username" : req.body.username, "password" : req.body.password };


     User.count(query, function(err, result) {

     if (err) throw err;

     if(result > 0){

         res.render('pages/success');

     } else{

         res.send("Incorrect credentials")

       // toastr.info('Are you the 6 fingered man?')

     }



     });



 }


};

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question