0

I am trying to insert array of objects inside array of objects in my mongoDB schema. This is how i want my schema to appear.

const CourseSchema = mongoose.Schema({
    categoryname: {
        type: String,
        required: "Course Category",
        min: 3,
        max: 100
    },
    coursename: {
        type: String,
        required: "Course Name",
        min: 3,
        max: 100
    },
    levels:
        [
            {
                levelid: Number,
                levelname: String,
                chapter:
                    [
                        {
                            chapternumber: Number,
                            chaptername: String,
                            content: String  //To be elaborated
                        }
                    ]
            }
        ]
});

My API which i have written looks like this:

exports.addcourse = (req, res) => {
    let levels = [];
    levels.push({
        levelid: req.body.levelid,
        levelname: req.body.levelname,
        chapter: [
            {
                chapternumber: req.body.chapternumber,
                chaptername: req.body.chaptername,
                content: req.body.content
            }
        ]
    })
    const newCourse = new Course({
        coursename: req.body.coursename,
        categoryname: req.body.categoryname,
        levels: levels
    });
    newCourse.save(function (error) {
        if (error) res.json({ message: 'could not add course because ' + error });
        res.json({ newCourse: newCourse });
    });
}

This works fine when i enter one level and one chapter, but gives an error when i enter multiple data. I am giving input from postman 'x-www'form-urlencoded'.

please help.

The error i get when i add one more levelid and levelname in postman

{
    "message": "could not add course because ValidationError: levels.0.levelid: Cast to Number failed for value \"[ '1', '2' ]\" at path \"levelid\", levels.0.levelname: Cast to String failed for value \"[ 'First Level', 'Second Level' ]\" at path \"levelname\""
}

The data i am trying to enter The data i am trying to enter[1]

4
  • What is the error that you are getting? Also please share the data on which you are getting the error. Commented Feb 27, 2020 at 5:41
  • This is the error i get when i try to add one more levelid and levelname, { "message": "could not add course because ValidationError: levels.0.levelid: Cast to Number failed for value \"[ '1', '2' ]\" at path \"levelid\", levels.0.levelname: Cast to String failed for value \"[ 'First Level', 'Second Level' ]\" at path \"levelname\"" } Commented Feb 27, 2020 at 5:45
  • I think something's not right in the data that you are trying to insert, can you please share the data once? Commented Feb 27, 2020 at 5:47
  • yeah i edited my question to add a screenshot of my postman Commented Feb 27, 2020 at 5:54

1 Answer 1

2

In postman, when you are sending the same key levelid twice, it converts it to array containing both the values. Like in your case, in req.body.levelid, you will receive [1, 2]. But in your schema, levelid is expecting a number value. Also, you are reading it wrong in your API code. Instead of getting levelid and chapter seperately, you can get the levels array in request body with values levelid, levelname and chapter. Similarly, chapter can be an array of objects. req.body.levels will look like this:

[{
  levelid: 1,
  levelname: "level1",
  chapter: [{
              chapternumber: 1,
              chaptername: "chapter1",
              content: "chapter1-content"
           }, {
              chapternumber: 2,
              chaptername: "chapter2",
              content: "chapter2-content"
           }] 
}]

Try this in postman by giving input in raw

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah giving input using Raw works great, but why not using the form-encoded.?
I tried using couple of for loops to but its giving output in a weird way. for (let i = 0; i < (req.body.levelid).length; i++) { for (let j = 0; j < (req.body.chapternumber).length; j++) { levels.push({ levelid: req.body.levelid[i], levelname: req.body.levelname[i], chapter: [{ chapternumber: req.body.chapternumber[j], chaptername: req.body.chaptername[j], content: req.body.content[j] }]})}}
Because the form-encoded in postman is not sending the desired body and that's why mongo is sending an error on insertion. If I am not wrong, each chapter array with values chapternumber, chaptername etc is associated with specific levelid and levelname and sending the values loosely like you are doing is not a correct way to do it. Rather than getting these values seperately, try getting "levels" array in request body as I have mentioned, and it will work fine.

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.