title: hashing password using brcrpt in "pre"
body: <p>i am trying to store a hash of the password entered in a form using bcrypt in mongodb</p>
  <p>this is the form</p>
  <pre><code>&lt;form action=&quot;/register&quot; method=&quot;POST&quot;&gt;
  &lt;label for=&quot;firstname&quot;&gt;Firstname&lt;/label&gt;
  &lt;input type=&quot;text&quot; name=&quot;firstname&quot; id=&quot;fname&quot; &gt;&lt;/p&gt;&lt;br/&gt;
  &lt;label for=&quot;lastname&quot;&gt;Lastname&lt;/label&gt;
  &lt;input type=&quot;text&quot; name=&quot;lastname&quot; &gt;&lt;/p&gt;&lt;br/&gt;
  &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
  &lt;input type=&quot;email&quot; name=&quot;email&quot; &gt;&lt;/p&gt;&lt;br/&gt;
  &lt;label for=&quot;&quot;&gt;Password&lt;/label&gt;
  &lt;input type=&quot;password&quot; name=&quot;password&quot; &gt;&lt;/p&gt;&lt;br/&gt;
  &lt;input type=&quot;submit&quot; value=&quot;submit&quot;&gt;
  </code></pre>
  
  <p>this is my mongoose schema</p>
  <pre><code>const userschema = new mongoose.Schema({   // define a schema  
      fname:{type:String,trim:true}, 
      lname:{type:String,trim:true},
      email:{type:String,lowercase:true,trim:true}, 
      pass:{type:String} 
  });
  </code></pre>
  <p>and this is my pre function that is to be performed before save is called in my handler</p>
  <pre><code>userschema.pre('save',async function(next){
      const user=this;
      console.log(user);                  //in this log the user object this is containing plaintext value of password
      await bcrypt.hash(user.pass, 8, function(err, hash) {
          if(err){throw new Error(err);}
          else{
              user.pass=hash;
              console.log(user);          //and in this log the user object is containing hashed value as expected
          }
      })
      next()}
      ); 
  </code></pre>
  <p>and this is my handler for saving the user in the database</p>
  <pre><code>app.post(&quot;/register&quot;,function(req,res){
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
          console.log(errors.array());
        return res.status(400).render(&quot;register&quot;,{ errors: errors.array() });
      }
      console.log(req.body);            //out of context question but for some reason only firstname and email are visible in req.body object can anybody body explain
      const usave=new user({
          fname:req.body.firstname,
          lname:req.body.lastname,
          email:req.body.email,
          pass:req.body.password
      })
      usave.save((err,doc)=&gt;{
          if(err){res.send(err);}
          else{
              res.send(&quot;success&quot;);}
      });
  });
  </code></pre>
  <p>when the route is handled the user is being saved succesfully but the password stored in the database is not being hashed.</p>
  <p>I did make user.pass=hash in the schema.pre('save') function but its not being stored in the  database as hash just being stored as plain text</p>
  <p>can anybody tell me what is wrong with this code?how do i save the hashpassword to database?</p>