响应中的变量未定义?

时间:2017-12-15 14:42:38

标签: javascript angularjs

所以我有一个问题一直困扰着我,我希望有人可以帮助我“升级”一点。 我在服务中有这个功能:

this.getChatStreams = function(callback) {
    $http.get(envService.read('apiUrl') + '/messaging/chat')
      .then(function(response) {
        callback(response);
      }).catch(function(response) {
        callback(response);
      });
  };

现在可行,是的,我知道我可以做一些更好的错误处理。但是在我的回电中:

chatService.getChatStreams(function(response) {
  $scope.emptychat = true;
  if (response.data.conv !== null) {
    $scope.emptychat = false;
    $scope.chats = response.data.conv;
    startChat();
  }
});

我在控制台中收到错误:

  

undefined不是对象(评估response.data.conv

response.data.conv但是为空,但未定义 - 所以我做错了什么?

/彼得

2 个答案:

答案 0 :(得分:0)

试试这个!

var response= $.parseJSON(response);

并访问

答案 1 :(得分:0)

为什么不跑:

  if (response.data.conv) {

这样你检查null和undefined,而不仅仅是null?