0

I am unable to insert data to MongoDB. I have following codes. I tried many similar answer but didn't work for me.

Schema - question.js

var questionsSchema = mongoose.Schema({
question: String,
quizdetails: String,
answers: {id:{type: String},text: { type: String }},
correctanswer: String,
feedback: String
});

Router - questions.js

router.post('/',jsonParser, function(req, res, next) {
 console.log(req.body);
}

Returns

{ question: 'quiz title',
  quizDetails: 'quiz description',
  answers:
   { '0': 'Answer 1',
     '1': 'Answer 2',
     '2': 'Answer 3',
     '3': 'Answer 4' },
  correctAnswer: 2,
  quizFeedback: 'Feedback' }

Callback

module.exports.createQuestion = function(newQuestion, callback) {
  newQuestion.save(callback);
}
2

1 Answer 1

0

I would make answers type to Array:

answers: Array

then Returns document should be (document we will insert in collection)

{ question: 'quiz title',
  quizDetails: 'quiz description',
  answers: ['Answer 1',
            'Answer 2',
            'Answer 3',
            'Answer 4'],
  correctAnswer: 2,
  quizFeedback: 'Feedback' }
Sign up to request clarification or add additional context in comments.

Comments

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.