My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Creating Nested Mongoose Schema / Model

Deactivated User's photo
Deactivated User
·Nov 24, 2018

I have this model in mind, so I made a json file out of it. When I wrote a model and tried saving a user from postman, it wouldn't save.

So how do I make a nested model out of this...

{
  "bio": {
    "full name": "Your fullname",
    "age": 23,
    "lga": "somewhere in nigeria",
    "blood group": "0+",
    "genotype": "AA",
    "height": "234.2''",
    "residential_addr": "your street number"
  },
  "birth_records": {
    "dob": "02-8-1990",
    "time_of_birth": "2 am",
    "hospital_name": "Federal Medical Center",
    "hospital_address": "44b Zamlek Ave.",
    "doctor_in_charge": "Philip Stanley",
    "midwives": [
      "Stella Vicent", "Oluchi Bridget", "Kate Henshaw"
    ]
  },
  "medical_records": {
    "current_health_report": "some health data",
    "doctor": "Dully Brown",
    "health_history": "pull up a file"
  },
  "educational_records": {
    "schools_attended": [
      "primary school 1993", "secondary school 1997"
    ],
    "certs": [
      "waec", "B.Sc"
    ],
    "skills": [
      "baking", "painting", "programming"
    ]
  },
  "contact_details": {
    "mobile_number": 234803493345,
    "email": "",
    "social_platforms": {
      "facebook": "facebook.com/edet",
      "twitter": "@edfon",
      "instagram": "@edfon"
    },
    "next_of_kin": {
      "number": 23476543234,
      "email": "",
      "name": "Udak Peter Mfon"
    }
  },
  "criminal_records": {
    "police_statements": "link to file",
    "offense": "link to file",
    "all_crimes": "link to file"
  },
  "relation_records": {
    "edit Kalu": {
      "location": "address 1",
      "number": 23456789032,
      "occupation": "hair stylist"
    },
    "Prince Kelechi": {
      "location": "address 2",
      "number": 23456789032,
      "occupation": "Photographer"
    },
    "Helen Paul": {
      "location": "address 3",
      "number": 23456789032,
      "occupation": "Actress"
    }
  },
  "financial_records": {
    "bank_statement": "link to file",
    "salary_draft": "link to file",
    "financial_book": "link to file",
    "assets": "list all assets and values"
  },
  "gov_related_services": {
    "employment": "federal ministry of works",
    "health_care": true,
    "is_retired": false
  }
}

This is the actual model file

const mongoose = require('mongoose')

const userSchema = new mongoose.Schema({
  bio: {
    firstName: { 
      type: String, 
      required: true,
      minlength: 5 
    },
    lastName: { 
      type: String, 
      required: true,
      minlength: 5 
    },
    email: {
      type: String,
      required: true,
      minlength: 12,
      unique: true
    },
    password: {
      type: String,
      required: true,
      minlength: 3
    },
    // phoneNumber: [String],
    nin: {
      type: Number,
      required: true,
      unique: true
    },
    age: {
      type: Number,
      required: true,
      minlength: 1,
    },
    height: {
      type: String,
      required: true,
    },
    genotype: {
      type: String,
      required: true,
      minlength: 2
    },
    blood_group: {
      type: String,
      required: true,
      minlength: 1
    },
    lga: {
      type: String,
      required: true,
      minlength: 4
    },
    residential_addr: {
      type: String,
      minlength: 8,
      required: true
    }
  }
})

const User = mongoose.model('User', userSchema)

module.exports = User

And this is the method for saving

const User = require('../model/user')

module.exports = {
  get_users (req, res) {
    // res.send('getting users')
    try {
      let user = User.find().select('-__v')
      res.status(200).send(user)
    } catch (error) {
    res.status(400).send('No users found in the database')
    }
  },

  // Register a new user
  add_new_user (req, res) {
    // search the database for a given user,
    let email = req.body.email
    let user = User.find({ email })

    //  if found return error
    if (user) return new Error(`Sorry, user with ${email} already exists`)

    // else save the new user and return details
    user = new User({
      bio: {
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        email: req.body.email,
        password: req.body.password,
        nin: req.body.nin,
        age: req.body.age,
        height: req.body.height,
        genotype: req.body.genotype,
        blood_group: req.body.blood_group,
        lga: req.body.lga,
        residential_addr: req.body.residential_addr
      }
    })

    user.save()
      .then(doc => {
        res.status(201).send(doc)
      })
      .catch(err => {
        res.status(400).send(err.message)
      })
  },

  // user login
  user_login (req, res) {
    res.send('logging in...')
  }
}