简体   繁体   中英

Angular GET from mongodb returns [object Object]

I'm trying to get data from a mongodb server but I keep getting [object Object] for some of the values. I've looked around and seen other people get the same issue but none of those solutions worked. Any and all help would be appreciated.

What's on the server,

[
{
"_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
      }
    ]
  }
]
}
]

The controller for the output page.

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

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

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

What I get back.

[{"_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]"]}]

Make sure that you JSON.stringify(data); on your server before you send it and after that use JSON.parse(data); to convert it to JS object.

There are a number of solutions to the problem. Since you know the objects are strings you could do this String($scope.questions[desiredQuestionIndex]) . If you would like to convert all the objects in the Array, JSON.stringify($scope.questions); .
See this question for more info Converting an object to a string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM