Save/design nested form to save/update data in mongodb via mongoose according to mongoose schema

蹲街弑〆低调 提交于 2020-01-23 12:58:28

问题


I have created the following mongoose schema for saving a quiz in mongodb -

var answerSchema = new Schema({
    answer: String,
    correct: Boolean
});

var questionSchema = new Schema({
    title: String,
    tag: String,
    chapterName: String,
    marks: Number,
    negativeMarks: Number,
    correctAnswerExplanation: String,
    hint: String,
    answers: [answerSchema]
});

var quizSchema = new Schema({
    courseId: Number,
    courseName: String,

    lectureId: Number,
    lectureName: String,

    popupTime: Number,
    teacherName: String,

    questions: [questionSchema]
});

var Quiz = mongoose.model('quiz', quizSchema );

I am able to store the hard coded values in the following way -

var quiz1 = new Quiz({
            courseId: 4,
            lectureId: 5008,
            popupTime: 3,
            teacherName: 'Charles Xavier',
            questions: [
                {
                    "title": "Which is the Capital of India " ,
                    "answers": [
                        { "answer": "Delhi", "correct": true },
                        { "answer": "Bangalore", "correct": false },
                        { "answer": "Mumbai", "correct": false },
                        { "answer": "Chennai", "correct":  false }
                    ]
                },
                {
                    "title": "Where is facebook hosted? " ,
                    "answers": [
                        { "answer": "Heroku", "correct": true },
                        { "answer": "Digital Ocean", "correct": false },
                        { "answer": "AWS", "correct": true },
                        { "answer": "SWF", "correct":  false }
                    ]
                }
            ]
        });

quiz1.save(function(err, q) {
            console.log("Saved ----");
            console.log(q);
            res.json(q);
        });

This works perfectly fine.

Now which is the best way to create a GUI form so that when the user fills in the details, it is converted to json and it is saved in mongodb via mongoose.

I have come up with the following form -

<form id="aj">
            <input type="text" name="questions[0]" value="" placeholder="Question Title">
                <input type="text" name="answers[0]" value="" placeholder="Answer 1">
                <input type="text" name="answers[1]" value="" placeholder="Answer 2">
                <input type="text" name="answers[2]" value="" placeholder="Answer 3">
                <input type="text" name="answers[3]" value="" placeholder="Answer 4">
            <button>Send</button>
        </form>

And on form submit I serialize the form data using this snipped -

var o = {};
        var a = $( "#aj" ).serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });

And in the express route handler I am saving it in the similar way - This is what I get in the request body via express -

{ questions: [ 'Q 1' ],
  answers: [ ' A 1', 'A 2', 'A 3', 'A 4' ] }

var quiz = req.body(i.e the above object)


var add = new Quiz(quiz);
    add.save(function(err, quiz) {
        if(err) {
            console.log(err);
        } else {
            console.log("Saved");
            console.log(quiz);
        }
    });

But I am getting the following error -

[TypeError: Cannot use 'in' operator to search for '_id' in Q 1]

How do I solve this, please help as I just started with mongodb and mongoose and I come from a SQL background.

Also, how should I design a form that takes inputs of multiple questions and each question can have multiple answers and each answer has a bit to indicate whether it is correct answer or not.

来源:https://stackoverflow.com/questions/26650304/save-design-nested-form-to-save-update-data-in-mongodb-via-mongoose-according-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!