我已经为添加问题定义了一个模式,就像
一样var mongoose = require('mongoose');
var questionSchema = new mongoose.Schema({
question_set : String,
questions:[{
question_id : String,
question_no : Number
}]
});
我想插入变量,例如ques_set = 'xyz'
和数组question_array = [['id_1',1],['id_2',2],['id_3',3]]
。
我使用此代码插入mongodb
var questions = require('../schemas/questions');
exports.testing = function(req,res){
if (!req.body) return res.sendStatus(400)
var ques_set = 'xyz';
var question_array = [['id_1',1],['id_2',2],['id_3',3]];
var data = question({ques_set,question_array);
data.save(function(err) {
if (err) throw err;
else {
console.log('Question Inserted');
res.send("Question Inserted");
}
});
};
这显示错误TypeError: object is not a function
。请帮帮我,我刚开始使用nodejs。感谢
答案 0 :(得分:1)
您需要创建一个与您的架构匹配的问题对象,如下所示:
var Question = require('../schemas/questions');
exports.testing = function(req,res){
if (!req.body) return res.sendStatus(400)
var ques_set = 'xyz';
var question_array = [
{
question_id: "id_1",
question_no: 1
},
{
question_id: "id_2",
question_no: 2
},
{
question_id: "id_3",
question_no: 3
}
];
var data = Question({question_set: ques_set, questions: question_array});
data.save(function(err) {
if (err) throw err;
else {
console.log('Question Inserted');
res.send("Question Inserted");
}
});
};