[英]Angular GET from mongodb returns [object Object]
我試圖從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]"]}]
確保您使用JSON.stringify(data); 在發送之前和之后在服務器上使用JSON.parse(data); 將其轉換為JS對象。
有許多解決該問題的方法。 由於您知道對象是字符串,因此可以執行String($scope.questions[desiredQuestionIndex])
。 如果要轉換數組中的所有對象,請JSON.stringify($scope.questions);
。
有關更多信息,請參見此問題。 將對象轉換為字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.