多个输入行为MongoDB创建数据数组

时间:2017-02-25 23:13:02

标签: javascript mongodb forms object database

所以这是我的另一次,我正在经历一些不断头痛的问题。 这是代码:

adminForm: function(){
  var div = document.getElementById('main');
  var form = document.createElement('form');
  form.action = "/api/players";
  this.createInput(form, "question", "text", "question", "Your question: ", "50");
  this.createInput(form, "option", "text", "option", "A:", "20");
  this.createInput(form, "option", "text", "option", "B:", "20");
  this.createInput(form, "option","text", "option", "C:", "20");
  this.createInput(form, "option", "text", "option", "D:", "20");
  this.createInput(form, "correct", "text", "correct", "Correct Answer", "20");
  this.createInput(form, "category", "text", "category", "Category", "20");

var allAnswers = function(){
  var values = [];
  var arr = document.getElementsByClassName('option');
  for(var element of arr){
    e.target.option.value;
  }
}

form.onsubmit = function(e){

  e.preventDefault();
  var newQuestion = {
    questionString: e.target.question.value,
    possibleAnswers: allAnswers(),
    correctAnswer: e.target.correct.value,
    category: e.target.category.value
  }

  var allQuestions = new Questions();
  allQuestions.add(newQuestion, function(data){
    console.log(data);
  })
}

我的意图是使用“option”类(类在createInput的第二个属性下)从所有输入中获取值,并将它们打包在一个数组中,该数组是我的“possibleAnswers”键的值问题对象。到目前为止,而不是我得到这个......

正确的对象

{
"_id": "58b20884f001b3aecec5c580",
"questionString": "A hitman was hired to dash the Olympic dreams of         which athlete?",
"possibleAnswers": [
  "Tonya Harding",
  "Kristi Yamaguchi",
  "Jennifer Capriati",
  "Nancy Kerrigan"
],
  "category": "Sport",
  "correctAnswer": "Nancy Kerrigan"
},

和错误的

 {
 "_id": "58b208e5556cd0fda30d99a5",
 "questionString": "What's your name?",
 "correctAnswer": "Bree",
 "category": "general",
 "possibleAnswers": {}
 }

0 个答案:

没有答案