Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular $http.get "Uncaught SyntaxError: Unexpected token )" No file reference, let alone a line number

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.

like image 275
E-Madd Avatar asked Nov 02 '22 00:11

E-Madd


2 Answers

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.

like image 190
allenhwkim Avatar answered Nov 11 '22 03:11

allenhwkim


  • 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.

like image 42
Fabio Bonfante Avatar answered Nov 11 '22 03:11

Fabio Bonfante