Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can create user in express.js using mongoose

I am trying to create a user in express.js using mongoose. I tried everything but i only get lastName and password i don’t know what happened. even in req.body.firstName also have data in but in newUser i only get two value lastName and password

let newUser = new User({
  firstName: req.body.firstName,
  lastName: req.body.lastName,
  email: req.body.email,
  password: req.body.password,
  phone: req.body.phone,
})

bcrypt.genSalt(10, (err, salt) => {
  bcrypt.hash(newUser.password, salt, (err, hash) => {
    if (err) {
      res.status(500).json({ error: err});
    } else{
      newUser.password = hash;
      newUser.save()
       .then(res => {
         res.status(200).json({ message: 'Successfullt created'   })
      })
      .catch((err) => {
        res.status(500).json({ error: err});
      })
   }
 })

})

Model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const mongoose = require('mongoose')
const { Schema } = mongoose

const userModel = new Schema({
   firtName: {
     type: String,
     required: true
   },
   lastName: {
     type: String,
     required: true
   },
   email: {
     type: String,
     required: true
   },
   password: {
     type: String,
     required: true
   },
   phone: {
     type: Number,
     required: true
   }

})

User = mongoose.model('User', userModel)
module.exports = User

>Solution :

This is a typo. firtName should be firstName in your model. Try this:

const userModel = new Schema({
   firstName: {
     type: String,
     required: true
   },
   ...
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading