【问题标题】:Submit all checked radio buttons提交所有选中的单选按钮
【发布时间】:2018-02-05 02:39:08
【问题描述】:

我正在尝试在 MEAN 堆栈的帮助下制作一个测验应用程序。尝试所有问题后,将有提交按钮来提交所有选中的单选按钮。就像选项 1 它应该是 1,对于选项 2 它应该是2.目前我的答案猫鼬模型看起来像这样-

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

})

我是否应该对猫鼬模型进行任何更改,因为提交后我必须将用户答案与正确答案进行比较。我觉得用户答案和正确答案字段应该是一个数组,以便所有选中的问题选项都可以存储一个one.另一方面,我将如何一次提交所有问题的所有测试数据。我的 angularjs 控制器功能逻辑应该是什么样的。

【问题讨论】:

  • 最好不要在 answerSchema 中包含 correctAnswer 而是将它放在单独的集合中,因为它会为每个用户在数据库中增加空间

标签: javascript angularjs node.js mongodb


【解决方案1】:

实际上,您可以为所有 angular 的测试问题提供一个对象数组。每道题做完后,继续往这个数组推送对象。

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

最后,当测试完成后,将其提交给 API。另一方面,保持架构结构化。不要保留电子邮件等冗余数据。您可以将其简化如下。

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

【讨论】:

  • 但是如果用户再次更改选项会怎样。
  • 您对本地阵列上的每个答案都有 questionId。所以基本上在推入数组之前,您可以过滤元素。如果该 questionId 的答案已经存在,则使用索引更新同一个对象。过滤器参考:w3schools.com/jsref/jsref_filter.asp
  • 谢谢!你能看看这个吗?这些模型组织正确吗? jsfiddle.net/rahuljhawar88/s3qxpswm
  • 看起来不错。但是 totalCorrectAnswers 和 totalWrongAnswers 都不是必需的。只有一个就足够了。一旦您拥有其中任何一项,就可以在运行时计算另一个。
【解决方案2】:

最好不要在 answerSchema 中包含 correctAnswer 而是将其放在单独的集合中,因为它会为每个用户在数据库中增加额外空间

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
    },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多