来自mongodb的Angular GET返回[object Object]

时间:2015-06-29 04:31:34

标签: javascript angularjs mongodb

我正在尝试从mongodb服务器获取数据,但我不断获取某些值的[object Object]。我环顾四周,看到其他人得到同样的问题,但这些解决方案都没有奏效。任何和所有的帮助将不胜感激。

服务器上有什么,

[
{
"_id": "5590c890a0bc61972f636f95",
"name": "Quiz",
"info": "Info",
"questions": [
  {
    "question": "How about 0?",
    "type": "radio",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 1?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 2?",
    "type": "radio",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 3?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 4?",
    "type": "text",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 5?",
    "type": "text",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 6?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 7?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 8?",
    "type": "text",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 9?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 10?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 11?",
    "type": "radio",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 12?",
    "type": "text",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 13?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 14?",
    "type": "radio",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 15?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 16?",
    "type": "radio",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 17?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 18?",
    "type": "text",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  },
  {
    "question": "How about 19?",
    "type": "check",
    "answers": [
      {
        "answer": 0
      },
      {
        "answer": 1
      },
      {
        "answer": 2
      },
      {
        "answer": 3
      }
    ]
  }
]
}
]

输出页面的控制器。

angular.module('quizApp')
  .controller('DispTestCtrl', function ($scope, $http) {
    $scope.questions = [];

    $http.get('/api/tests').success(function(data) {
      $scope.questions = data;
  });

  console.log($scope.questions);
});

我得到了什么。

[{"_id":"558f523a609aba42bf06b807",
"name":"Quiz",
"info":"Info",
"questions":["[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]","[object Object]"]}]

2 个答案:

答案 0 :(得分:0)

确保你JSON.stringify(数据);在发送之前在服务器上,之后使用JSON.parse(data);将其转换为JS对象。

答案 1 :(得分:0)

这个问题有很多解决方案。由于您知道对象是字符串,因此您可以执行此操作String($scope.questions[desiredQuestionIndex])。如果要转换数组中的所有对象JSON.stringify($scope.questions);
有关详细信息Converting an object to a string

,请参阅此问题