简体   繁体   中英

Submit all checked radio buttons

I'm trying to make a quiz app with the help of MEAN stack.After all the questions are attempted there will be submit button to submit all the checked radio buttons.Like for option1 it should be 1,for option2 it should be 2.Currently my answer mongoose model looks like this-

const answerSchema = new Schema({

                              userEmail: {
                                type: String, require:true
                              },
                              testId: {
                                type: String, require:true
                              },
                              questionId: {
                                type: String, require:true
                              },
                              userAnswer: {
                                type: String
                              },
                              correctAnswer: {
                                type: String, require:true
                              },
                              timeTakenEach: {
                                type: Number,
                                default: 1
                              }  //insecs

})

Should I make any changes to the mongoose model because after submission I have to compare the useranswer with the correct answer.I feel like the useranswer and correctanswer fields should be an array so that all the questions checked option can be stored one by one.On the other hand how will I submit all the test data at once for all questions.What should my angularjs controller function logic be like.

You can actually have an array of objects for all the test questions in angular. After the completion of every question, keep pushing object to this array.

[
  {
    questionId: 123,
    userAnswer: 1,
    ...
  },
  {
    questionId: 123,
    userAnswer: 1,
    ...
  },
];

Finally when the test is complete, submit it to the API. On the other hand, keep your schema structured. Do not keep redundant data like email. You can simplify this as follows.

answerSchema = {
  userInfo: {
    name: 'abc',
    email: 'abc@xyz.com',
    attemptedOn: ...,
    ...
  },
  testMetaData: {
    testId: 1,
    testName: 'ABC Test',
    ...
  },
  attemptedAnswers: [{
    questionId: 1,
    attemptedAnswer: 2
  },
  ...
  ]
};

Better don't include correctAnswer in the answerSchema instead have it in the separate collection because it will extra space in the DB for each user

const answerSchema = new Schema({

    userEmail: {
        type: String, require: true
    },
    testId: {
        type: String, require: true
    },
    questionId: {
        type: String, require: true
    },
    userAnswer: {
        type: String
    },
    timeTakenEach: {
        type: Number,
        default: 1
    }  //insecs

});

const questionAnswer = new Schema({
    questionId: {
        type: String, require: true
    },
    correctAnswer: {
        type: String, require: true
    },
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM