Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在引用文档中插入数据?_Node.js_Mongodb_Mongoose_Mongoose Populate - Fatal编程技术网

Node.js 在引用文档中插入数据?

Node.js 在引用文档中插入数据?,node.js,mongodb,mongoose,mongoose-populate,Node.js,Mongodb,Mongoose,Mongoose Populate,我正在尝试将数据插入myMongoDB数据库,我使用的是引用文档,但我找不到任何方便的方法来插入数据(而获取非常方便)。 请在标记副本之前阅读,我在发布之前已经搜索了整个Stackoverflow和github 这是我的考试模式 const ExamSchema = new mongoose.Schema({ name: {}, description: {}, allowedTime: {}, subject: {}, assignedInCharge:

我正在尝试将数据插入myMongoDB数据库,我使用的是引用文档,但我找不到任何方便的方法来插入数据(而获取非常方便)。
请在标记副本之前阅读,我在发布之前已经搜索了整个Stackoverflow和github
这是我的考试模式

const ExamSchema = new mongoose.Schema({
    name: {},
    description: {},
    allowedTime: {},
    subject: {},
    assignedInCharge: {},
    questions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'MCQuestion' }]
});
下面是我的问题模式:

const MultipleChoiceQuestionSchema = new mongoose.Schema({
    question: {},
    answerOptions: [{}],
    correctAnswer: {},
    marksForCorrectAnswer: {},
    negativeMark: {},
    difficulty: {}
});
我的问题是:mongoose中是否有内置方法将文档中缺少的数据放入数据库中(我已经在那里搜索了一千次)。

我的输入如下:

{
    "name": "Sample Exam 1",
    "description": "IDK if this will work",
    "allowedTime": 123445666,
    "subject": "testsub1",
    "assignedInCharge": "someincharge",
    "questions": [
        {
            "question": "this is que1",
            "answerOptions": ["this is op 1", "this is op 2", "op 3", "op 4"],
            "correctAnswer": "this is op 1",
            "marksForCorrectAnswer": 4,
            "negativeMark": 1,
            "difficulty": 3
        },
        {}, {}, {}
    ]
}
(很抱歉有这么长的代码片段)我尝试将数据插入数据库的方法是:

router.post('/admin/createExam', (request, response) => {

    questions = request.body.questions;
    var idarray = [];

    questions.forEach(function(element) {
        var question = new MCQuestion(element);
        question.save().then((doc) => idarray.push(doc._id), (error) => console.log(error));
    });

    var body = _.pick(request.body, ['Everything Except Questions']);
    body.questions = idarray;

    var exam = new Exam(body);  

    exam.save().then(() =>{
        response.send(exam);
    }).catch((error) => {
        response.status(400).send(error);
    });

});
我能够成功地将问题保存到问题集合中,但未正确保存考试集合


请帮帮我,我已经在这上面花了好几个星期的时间了。

没关系,我明白了,改变API方法使它变得很好

router.post('/admin/createExam', (request, response) => {

    var body = _.pick(request.body, ['name', 'description', 'allowedTime', 'subject', 'assignedInCharge']);
    var exam = new Exam(body);

    request.body.questions.forEach(function(element) {
        element.exam = exam._id;
        var question = new MCQuestion(element);
        exam.questions.push(question._id);
        question.save().catch((error) => console.log(error));
    });

    exam.save().then(() =>{
        response.send(exam);
    }).catch((error) => response.status(400).send(error));

});

没关系,我明白了,改变API方法使它变得很好

router.post('/admin/createExam', (request, response) => {

    var body = _.pick(request.body, ['name', 'description', 'allowedTime', 'subject', 'assignedInCharge']);
    var exam = new Exam(body);

    request.body.questions.forEach(function(element) {
        element.exam = exam._id;
        var question = new MCQuestion(element);
        exam.questions.push(question._id);
        question.save().catch((error) => console.log(error));
    });

    exam.save().then(() =>{
        response.send(exam);
    }).catch((error) => response.status(400).send(error));

});