3

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]"]}]
manmon42
  • 319
  • 3
  • 14
  • Can you check the network tab to verify whether the data is already incorrect when it is sent from the server or it's a problem on client side? – Icycool Jun 29 '15 at 04:34
  • Possibly JSON.parse(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – Shawn Erquhart Jun 29 '15 at 04:35
  • Console.log() dont print all object if response objects are long. So please check in charles, postman extension or return array in json response. If you are using php then use print_r() or var_dump() ; – Nishchit Jun 29 '15 at 05:03
  • I checked in the network tab in the dev tools of chrome, the data is incorrect when it reaches the client. – manmon42 Jun 29 '15 at 05:08

2 Answers2

0

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.

user1
  • 1,035
  • 1
  • 9
  • 17
0

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

Community
  • 1
  • 1
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109