Javascript 提交所有选中的单选按钮

Javascript 提交所有选中的单选按钮,javascript,angularjs,node.js,mongodb,Javascript,Angularjs,Node.js,Mongodb,我正在尝试借助MEAN stack制作一个测验应用程序。在尝试所有问题后,将有一个提交按钮来提交所有选中的单选按钮。对于选项1,它应该是1,对于选项2,它应该是2。目前我的答案mongoose模型如下所示- const answerSchema = new Schema({ userEmail: { type: String, require:true

我正在尝试借助MEAN stack制作一个测验应用程序。在尝试所有问题后,将有一个提交按钮来提交所有选中的单选按钮。对于选项1,它应该是1,对于选项2,它应该是2。目前我的答案mongoose模型如下所示-

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

})

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

您实际上可以为angularjs中的所有测试问题创建一个对象数组。完成每个问题后,继续将对象推到此数组

[
  {
    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
  },
  ...
  ]
};
最好不要将correctAnswer包含在answerSchema中,而是将其放在单独的集合中,因为它会为每个用户在数据库中增加额外的空间

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

最好不要将correctAnswer包含在answerSchema中,而是将其放在单独的集合中,因为这会为每个用户在数据库中增加空间,但如果用户再次更改该选项该怎么办。您的本地数组中的每个答案都有questionId。因此,基本上在进入数组之前,您可以过滤元素。如果该问题ID的答案已经存在,则使用索引更新同一对象。过滤器参考:谢谢!你能看一下这个吗?这些模型组织正确吗。看起来不错。但totalCorrectAnswers和TotalErrorAnswers都不是必需的。只有一个就够了。一旦有了其中的任何一个,就可以在运行时计算另一个。