1

This is my model profile.js

var mongoose = require('mongoose');

const ProfileSchema = mongoose.Schema({
    educationinfo: [{
        universityname:
        {
            type: String,
            required: true
        },
        degree:
        {
            type: String,
            required: true
        },
        coursecompletionyear:
        {
            type: String,
            required: true
        },
        collegename:
        {
            type: String,
            required: true
        },
        specialization:
        {
            type: String,
            required: true
        },
        marks:
        {
            type: String,
            required: true
        },
        courselevel:
        {
            type: String,
            required: true
        }
    }]
});

const Profile = module.exports = mongoose.model('Profile', ProfileSchema);

This is my route.js post function

router.post('/addprofiledetails', function(req, res, next) {
  let newProfile = new Profile({
    $educationinfo:[{
        universityname:req.body.universityname,
        degree:req.body.degree
    }]
  });
  newProfile.save((err, profile) => {
    if (err) {
        res.json({ msg: 'Failded to add profiledetails' });
    } else {
        res.json({ msg: 'successfully add profile details' });
    }
  });
});

I got success msg in post function but the data not added in mongodb. i don't know where i did mistake .please help me.

In mongoDB I got data like,

{
    "educationinfo": [],
    "_id": "5bed14b93a107411a0334530",
    "__v": 0
}

I want details inside educationinfo, please help.

3 Answers 3

1

You need to change schema definition and query.

1.remove required from schema Or apply required to those field that you must provide value.

educationinfo: [{
        universityname:
        {
            type: String,
           // required: true
        },
        degree:
        {
            type: String,
            //required: true
        },
        coursecompletionyear:
        {
            type: String,
           // required: true
        },
        collegename:
        {
            type: String,
           // required: true
        },
        specialization:
        {
            type: String,
            //required: true
        },
        marks:
        {
            type: String,
           // required: true
        },
        courselevel:
        {
            type: String,
           // required: true
        }
    }]

2.change $educationinfo with educationinfo

educationinfo:[{
    universityname:req.body.universityname,
    degree:req.body.degree
    }]
Sign up to request clarification or add additional context in comments.

Comments

1

Since you marked the properties of educationinfo as required, you need to provide them when you create an instance of Profile. If you don't want to do that you need to remove the required property from those properties that you won't be supplying on instance creation like below:

const mongoose = require('mongoose');

const ProfileSchema = mongoose.Schema({
    educationinfo: [{
        universityname:
        {
            type: String,
            required: true
        },
        degree:
        {
            type: String,
            required: true
        },
        coursecompletionyear:
        {
            type: String
        },
        collegename:
        {
            type: String
        },
        specialization:
        {
            type: String
        },
        marks:
        {
            type: String
        },
        courselevel:
        {
            type: String
        }
    }]
});

const Profile = module.exports = mongoose.model('Profile', ProfileSchema);

After making those changes, you need to make one more change in your POST route, change $educationinfo to educationinfo

router.post('/addprofiledetails', function(req, res, next) {
  const newProfile = new Profile({
    educationinfo:[{
        universityname: req.body.universityname,
        degree: req.body.degree
    }]
  });
  newProfile.save((err, profile) => {
    if (err) {
       res.json({ msg: 'Failded to add profiledetails' });
    } else {
       res.json({ msg: 'successfully add profile details' });
    }
  });
});

Comments

0

The data you insert is incomplete. The properties in your schema marked as required: true need to be inserted aswell. Because you do not meet the schema requirements, it is failing.

1 Comment

i can't get it. can u tell breifly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.