title: How do I check if login credentials is present in mongodb using nodejs?
body: <p>I am making a small login and registration using node.js and mongodb with dependencies such as express, mongoose and body-parser.
  I am not able to make the login part work. In my case, even with the wrong credentials, the login takes place. Please look at my code and help me with it.</p>
  
  <p>This is my index.js node.js code.    </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const bodyParser = require('body-parser')
  const mongoose = require("mongoose");
  var app = express();
  var path = require('path');
  app.use('/static', express.static(path.join(__dirname, 'public')));
  
  app.use(bodyParser.urlencoded({extended: false}));
  
  mongoose.connect("mongodb://localhost:27017/test", { useNewUrlParser: true });
  
  var Schema=mongoose.Schema;
  
  var registerSchema = new Schema({
      uname:String,
      psw:String
  });
  var reg = mongoose.model('reg',registerSchema);
  
  app.get("/", (req,res)=&gt;{
      res.sendFile(path.join(__dirname + '/index.html'));
  });
  
  
  
  app.post("/register", (req,res)=&gt;{
          const user= new reg(req.body);
          if(reg.findOne({uname:req.body.name})){
              res.send("Username is taken");
          }
          else{
          user.save()
          .then(user =&gt; {
              res.send("User added succesfully.")
          })
          .catch(err =&gt; {
              res.status(400).send("Unable to create user.")
          });
      }
  }) 
  
  
  app.get("/login", (req,res)=&gt;{
      reg.findOne({uname:req.body.uname, psw:req.body.psw}, function(err,user)
      {
          if(err)
          {
          res.send("User not found.")
          }
          res.send("Logged in succesfully.")
      }
  )});
  
  app.listen(3000, function(){
      console.log('Server started');
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;title&gt;Page Title&lt;/title&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="/register" method="POST"&gt;
          &lt;label for='uname'&gt;Username:&lt;/label&gt;
          &lt;input type="text" placeholder="Enter Username" name="uname" required&gt;
          &lt;br&gt;
          &lt;label for="psw"&gt;Password:&lt;/label&gt;
          &lt;input type="text" placeholder="Enter Password" name="psw" required&gt;
          &lt;br&gt;&lt;hr&gt;
          &lt;button type="submit" &gt;Register&lt;/button&gt;
      &lt;/form&gt;
      &lt;br&gt; &lt;br&gt;
  
      &lt;form action="/login" method="GET"&gt;
          &lt;label for='uname'&gt;Username:&lt;/label&gt;
          &lt;input type="text" placeholder="Enter Username" name="uname" required&gt;
          &lt;br&gt;
          &lt;label for="psw"&gt;Password:&lt;/label&gt;
          &lt;input type="text" placeholder="Enter Password" name="psw" required&gt;
          &lt;br&gt;&lt;hr&gt;
          &lt;button type="submit" &gt;Login&lt;/button&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>