简体   繁体   English

来自mongodb的角度GET返回[object Object]

[英]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. 我试图从mongodb服务器获取数据,但是我一直在获取[object Object]的某些值。 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); 确保您使用JSON.stringify(data); on your server before you send it and after that use JSON.parse(data); 在发送之前和之后在服务器上使用JSON.parse(data); to convert it to JS object. 将其转换为JS对象。

There are a number of solutions to the problem. 有许多解决该问题的方法。 Since you know the objects are strings you could do this String($scope.questions[desiredQuestionIndex]) . 由于您知道对象是字符串,因此可以执行String($scope.questions[desiredQuestionIndex]) If you would like to convert all the objects in the Array, JSON.stringify($scope.questions); 如果要转换数组中的所有对象,请JSON.stringify($scope.questions); .
See this question for more info Converting an object to a string 有关更多信息,请参见此问题。 将对象转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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