我无法将数据插入MongoDB。我有以下代码。我尝试了许多类似的答案,但对我没有用。
架构 - question.js
var questionsSchema = mongoose.Schema({
question: String,
quizdetails: String,
answers: {id:{type: String},text: { type: String }},
correctanswer: String,
feedback: String
});
路由器 - questions.js
router.post('/',jsonParser, function(req, res, next) {
console.log(req.body);
}
返回
{ question: 'quiz title',
quizDetails: 'quiz description',
answers:
{ '0': 'Answer 1',
'1': 'Answer 2',
'2': 'Answer 3',
'3': 'Answer 4' },
correctAnswer: 2,
quizFeedback: 'Feedback' }
回调
module.exports.createQuestion = function(newQuestion, callback) {
newQuestion.save(callback);
}
答案 0 :(得分:0)
我会将答案类型设为Array:
answers: Array
然后返回文档应该是(我们将在集合中插入文档)
{ question: 'quiz title',
quizDetails: 'quiz description',
answers: ['Answer 1',
'Answer 2',
'Answer 3',
'Answer 4'],
correctAnswer: 2,
quizFeedback: 'Feedback' }