微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在尝试从其他子文档中的子文档中保存一个数组,但它没有

如何解决我正在尝试从其他子文档中的子文档中保存一个数组,但它没有

我想保存这个,但猫鼬没有。这是 api、模型和控制台显示内容

newExam = async (req,res)=>{
    hasItUsernameTwo(req,res)
    const user = await User.findById(req.user),examData = JSON.parse(req.body.deepFormJSON),newExam = new Exam();
    console.log(examData)
    console.log(examData)
    newExam.title = examData.title
    newExam.description = examData.description
    newExam.author = user.username
    newExam.questions = []
    // para crear el array de objetos con preguntas
    if(Array.isArray(examData.questions.question)){
        examData.questions.question.forEach(e => {
            newExam.questions.push({
                question: e,})
        })
        for (var i = 0; i < examData.questions.question; i++) {
            newExam.questions[i].answers = []
        }
    } {
        newExam.questions.push({
            question: examData.questions.question
        })
        newExam.questions.answers = []
    }
    // para crear el array con las respuestas de cada pregunta
    // const get = examData.questions.answers.answer.forEach(async (e,i,array) => {
    //     if(Array.isArray(examData.questions.question)){
    //         newExam.questions[i].answers.push({answer: e})
    //     } {
    //         newExam.questions.answers.push({answer: e})
    //     }
    //     // return newExam.save()
    // });
    // await Promise.all(get)
    for (const [i,e] of examData.questions.answers.answer.entries()){
        if(Array.isArray(examData.questions.question)){
            newExam.questions[i].answers.push({answer: e})
            newExam.questions[i].markModified("answers")
        } {
            newExam.questions.answers.push({answer: e})
            newExam.markModified("answers")
        }
    }
    // for (const [i,e] of examData.questions.answers.correct.entries()){
    //     if(Array.isArray(examData.questions.question)){
    //         for(const [index,element] of newExam.questions[i].answers.entries()){

    //         }
    //     } {
    //         for(const [index,element] of newExam.questions.answers.entries()){

    //         }
    //     }

    // }
    console.log(newExam.questions.answers)
    // para crear el array con los valores buleanos de cada pregunta
    // await newExam.save();
    // await newExam.validate();
    // user.exams.push(newExam._id)
    // await user.save()
    console.log(newExam)
    // res.redirect(`exams/${newExam._id}`)
    res.redirect(`/`)
}

这是我的模型:

answerSchema = new Schema({
    answer: String,correct: String
}),questionsSchema = new Schema({
    question: {
        type: String,required: "The questions are required"
    },answers: [answerSchema]
}),examSchema = new Schema({
    title: {
        type: String,required: true
    },description: {
        type: String,author: { 
        type: String,ref: "User",required: `The author is required.`
    },questions: [questionsSchema],usersDone: [{
        type: Schema.ObjectId,}]
},{
    timestamps: true,versionKey: false
});

这就是控制台显示内容

{
  title: 'JavaScript',description: 'An Exam For JavaScript Users',questions: { question: 'What is JavaScript?',answers: { answer: [Array] } }
}
[
  { answer: 'An extension for Java' },{ answer: 'A programming language' },{ answer: 'Idk,but it has 14 million of users' },{ answer: 'The Snake Game' }
]
{
  usersDone: [],_id: 60fa9b14ac5f133b70fa2ded,questions: [
    {
      _id: 60fa9b14ac5f133b70fa2dee,question: 'What is JavaScript?',answers: []
    }
  ],title: 'JavaScript',author: 'AlejandroArellano'
}

我已经多次尝试解决这个问题,但它并没有保存答案!我已经在 mongo 上查看了我的数据库,但它没有出现在那里!!!

解决方法

为了便于阅读,我粘贴了您的代码片段 here

在第 42 行中,您在 if 块结束后创建了一个块 ({}),没有任何 elseif 或任何东西,我不确定这是原因但值得调查一下

在第 43 行中,您正在修改 newExam.questions.answers,但在第 44 行中,您将 newExam.answers 标记为已修改。我猜您将错误的子文档标记为已修改。尝试纠正并让我知道它是否已修复(或未修复!)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。