5

I'm at my rope's end on this one. I am getting this error in seemingly random places in my app. Raven reports them from a couple dozen locations but I can only replicate a couple locally. It seems to me that the problem has something to do with the parsing of the JSON response but the responses are valid.

In my Angular service...

...
getThread: function(id, success, error) {
    $http.get('/message/'+id).success(function(data){
        success(data);
    }).error(error);
}
...

In my Express controller...

...
res.json(mssgs);
...

Here's a sample response...

[
  {
    "id": 79,
    "body": "test",
    "senderArchived": false,
    "recipientArchived": false,
    "createdAt": "2014-04-17T01:44:46.762Z",
    "updatedAt": "2014-04-17T01:44:46.762Z",
    "RootMessageId": 69,
    "SenderId": 164050,
    "RecipientId": 154040,
    "sender": {
      "username": "boca",
      "id": 164050,
      "primaryMedium": null
    },
    "recipient": {
      "username": "quimby",
      "id": 154040,
      "primaryMedium": {
        "id": "186",
        "type": "image",
        "nativeURL": "https://domain/imageurl.jpg",
        "mediumURL": "https://domain/imageurl.jpg",
        "smallURL": "https://domain/imageurl.jpg",
        "createdAt": "2014-04-21T15:52:10.927Z",
        "updatedAt": "2014-04-21T15:52:10.947Z",
        "CommentId": null,
        "EventId": null,
        "UserId": 154040,
        "PostId": null,
        "MessageId": null,
        "MediaFolderId": null
      }
    },
    "messageMedia": []
  }
]

In both Chrome and Safari this results in error "Uncaught SyntaxError: Unexpected token )"

Here's the request headers from Chrome...

Remote Address:127.0.0.1:3001
Request URL:https://localhost:3001/message/69
Request Method:GET
Status Code:200 OK

Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ja;q=0.6
Connection:keep-alive
Cookie:streamLoc=%7B%22distance%22%3A-1%2C%22locName%22%3A%22%22%7D; usePostLocation=yes; connect.sid=s%3AhX37rupUct2jut4yApN1GIH9.n5nPURTMXl5OKd46rMqeRc4bg1Q%2F%2Bky0El2r%2BcBvC8c; user=%7B%22id%22%3A154040%2C%22role%22%3A%7B%22bitMask%22%3A32%2C%22title%22%3A%22admin%22%7D%2C%22username%22%3A%22quimby%22%2C%22emailVerified%22%3Atrue%2C%22verified%22%3Atrue%7D
Host:localhost:3001
Referer:https://localhost:3001/messages
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36

Response Headers
Connection:keep-alive
Content-Length:1115
Content-Type:application/json; charset=utf-8
Date:Tue, 29 Apr 2014 02:41:47 GMT
ETag:"485872145"
X-Powered-By:Express

Every other similar question has pointed to JSONP or actual syntax problems but I'm not using JSONP and there are not any code syntax problems.

5
  • A minification problem? Try running with use strict? Commented Apr 29, 2014 at 3:09
  • Nope. Strict mode enabled already. No minification. Commented Apr 29, 2014 at 3:12
  • 2
    does .error() expect a function? Commented Apr 30, 2014 at 2:31
  • Seconding @richgreen. Is the error argument in your .error() call a function? Edit: Never mind, I see it. You're passing in callbacks to the getThread function. Can you post the code for your success and error functions? There may be a syntax issue there, who knows. Commented Nov 18, 2014 at 7:26
  • Download the non-minified version of AngularJS and debug the soure code. Commented Nov 19, 2014 at 19:48

3 Answers 3

1
+50

Since I don't know your exact situation, I can't provide the exact anwer. However, this is my try.

I see that your service is not returning anything, but processing success and error functions within it.

getThread: function(id, success, error) {
    $http.get('/message/'+id).success(function(data){
        success(data);
    }).error(error);
}
  1. Is your error(error) is correct? I assume you are passing error function as a parameter, but I see inconsistency between success(data) and error

  2. This is recommendation. I think you should change your service only return promise, and do not process anything. So, it should look like this.

    getThread: function(id) {
       return $http.get('/message/'+id);
    }
    

    Then your controller handles success and error, so that you can see all success messages and error messages. So, controller will have a code like the following

    MyService.getThread(id).then( 
      function(data) {....}, 
      function(error) {....}
    }
    

Having data processing logic in your controller will give you much more difficulty in debugging.

I would recommend you to process http data in your controller, not in a service.

Sign up to request clarification or add additional context in comments.

Comments

1
  • Your error seems related to some JSon parsing function
  • Your sample response header say "application/json; charset=utf-8"

Given this, looks that you don't need to use any parsing function, something is missing.

If you can not always reproduce it, I would enable recording network calls, so you can see retrieve exactly what response(s) give troubles.

Comments

0

Are you passsing a null as id in

getThread: function(id, success, error) {
    $http.get('/message/'+id).success(function(data){
        success(data);
    }).error(error);
}

That would give you a syntax error when resolving $http.get('/message/'+)

Insert a test for null before the $http call and wait for it to be hit.

2 Comments

actually null is casted to string..String('/message/'+null) = 'message/null'
Yep you are right. An empty string would do the job though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.