Advertisement
zircoz

qwerty

Jul 24th, 2021
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. newExam = async (req, res)=>{
  2.     hasItUsernameTwo(req,res)
  3.     const user = await User.findById(req.user),
  4.     examData = JSON.parse(req.body.deepFormJSON),
  5.     newExam = new Exam();
  6.     console.log(examData)
  7.     console.log(examData)
  8.     newExam.title = examData.title
  9.     newExam.description = examData.description
  10.     newExam.author = user.username
  11.     newExam.questions = []
  12.     // para crear el array de objetos con preguntas
  13.     if(Array.isArray(examData.questions.question)){
  14.         examData.questions.question.forEach(e => {
  15.             newExam.questions.push({
  16.                 question: e,
  17.             })
  18.         })
  19.         for (var i = 0; i < examData.questions.question; i++) {
  20.             newExam.questions[i].answers = []
  21.         }
  22.     } {
  23.         newExam.questions.push({
  24.             question: examData.questions.question
  25.         })
  26.         newExam.questions.answers = []
  27.     }
  28.     // para crear el array con las respuestas de cada pregunta
  29.     // const get = examData.questions.answers.answer.forEach(async (e, i, array) => {
  30.     //     if(Array.isArray(examData.questions.question)){
  31.     //         newExam.questions[i].answers.push({answer: e})
  32.     //     } {
  33.     //         newExam.questions.answers.push({answer: e})
  34.     //     }
  35.     //     // return newExam.save()
  36.     // });
  37.     // await Promise.all(get)
  38.     for (const [i, e] of examData.questions.answers.answer.entries()){
  39.         if(Array.isArray(examData.questions.question)){
  40.             newExam.questions[i].answers.push({answer: e})
  41.             newExam.questions[i].markModified("answers")
  42.         } {
  43.             newExam.questions.answers.push({answer: e})
  44.             newExam.markModified("answers")
  45.         }
  46.     }
  47.     // for (const [i, e] of examData.questions.answers.correct.entries()){
  48.     //     if(Array.isArray(examData.questions.question)){
  49.     //         for(const [index, element] of newExam.questions[i].answers.entries()){
  50.  
  51.     //         }
  52.     //     } {
  53.     //         for(const [index, element] of newExam.questions.answers.entries()){
  54.  
  55.     //         }
  56.     //     }
  57.  
  58.     // }
  59.     console.log(newExam.questions.answers)
  60.     // para crear el array con los valores buleanos de cada pregunta
  61.     // await newExam.save();
  62.     // await newExam.validate();
  63.     // user.exams.push(newExam._id)
  64.     // await user.save()
  65.     console.log(newExam)
  66.     // res.redirect(`exams/${newExam._id}`)
  67.     res.redirect(`/`)
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement