0

So I have an issue that keeps bugging the hell our of me I was hoping that someone could help me "level up" a bit. I have this function in a service:

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

Now that works, yes I'm aware I can do some nicer error handling. However in my call back:

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

I get an error in the console:

undefined is not an object (evaluating response.data.conv)

response.data.conv however is null, but not undefined - so what am I doing wrong?

/Peter

Milo
  • 3,365
  • 9
  • 30
  • 44

2 Answers2

0

Try this !

var response= $.parseJSON(response);

and access

0

Why not just run:

  if (response.data.conv) {

That way you check both null and undefined, rather than just if null?

David Anthony Acosta
  • 4,766
  • 1
  • 19
  • 19