6

I have a method to process the response from my google javascript client (gapi):

var processResponse = function(response) {
              result._state = 'loaded';
              response._epoch = (new Date()).getTime();
              ...

A few times I have gotten the following error:

TypeError: Cannot assign to read only property '_epoch' of false
    at processResponse (http://0.0.0.0:9000/scripts/services/haparaapi.js:110:31)
    at wrappedCallback (http://0.0.0.0:9000/bower_components/angular-scenario/angular-scenario.js:20892:81)
    at http://0.0.0.0:9000/bower_components/angular-scenario/angular-scenario.js:20978:26
    at Scope.$eval (http://0.0.0.0:9000/bower_components/angular-scenario/angular-scenario.js:21967:28)
    at Scope.$digest (http://0.0.0.0:9000/bower_components/angular-scenario/angular-scenario.js:21796:31)
    at Scope.$apply (http://0.0.0.0:9000/bower_components/angular-scenario/angular-scenario.js:22071:24)
    at http://0.0.0.0:9000/bower_components/angular-gapi/modules/gapi-client.js:121:32
    at https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_GB.hE_reuZ6VdE.…/ed=1/am=AQ/rs=AGLTcCPj66Crj6soG8dKJE8lBSc_RPXXKA/cb=gapi.loaded_0:604:138
    at https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_GB.hE_reuZ6VdE.…/ed=1/am=AQ/rs=AGLTcCPj66Crj6soG8dKJE8lBSc_RPXXKA/cb=gapi.loaded_0:579:311
    at Object.<anonymous> (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_GB.hE_reuZ6VdE.…1/ed=1/am=AQ/rs=AGLTcCPj66Crj6soG8dKJE8lBSc_RPXXKA/cb=gapi.loaded_0:163:76) 

This bug doesn't usually happen so I haven't managed to log what the response actually looked like.

What does the error mean? Should I not be assigning values to the response?

Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
  • 2
    It means what it says... `_epoch` is a *read-only* property, therefore you *cannot* assign any value to it... – LcSalazar Oct 16 '14 at 20:15
  • Thanks - What's a read-only property? I didn't know javascript had them. I suppose the gapi must be using them to make sure their variables don't get clobbered? – Rusty Rob Oct 16 '14 at 20:16
  • http://stackoverflow.com/a/2428505/632088 - mentions object.seal & object.freeze - could that be the cause? – Rusty Rob Oct 16 '14 at 20:19
  • I tried playing around with the methods here: http://ejohn.org/blog/ecmascript-5-objects-and-properties/ but wasn't able to cause an error on a readonly property. I was using strict mode too. – Rusty Rob Oct 16 '14 at 20:24

1 Answers1

9

It looks like the issue is that your processResponse() callback is actually being given a value of false. So essentially you are trying to assign the _epoch property of a false value.

See: https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientRequest

From the manual:

The callback function which executes when the request succeeds or fails. jsonResp contains the response parsed as JSON. If the response is not JSON, this field will be false.

When you're running javascript in strict mode ('use strict'), it will raise a TypeError like you are experiencing:

'use strict';
var processResponse = function(response) {
    response._epoch = (new Date()).getTime();
};

processResponse(false);   // Uncaught TypeError: Cannot assign to read only property '_epoch' of false 

JSFiddle: http://jsfiddle.net/0tq6mobm/

Suggest that you check the value of response before trying to assign your timestamp to the response.

itsmejodie
  • 4,148
  • 1
  • 18
  • 20
  • awesome thanks! I wasn't using strict mode correctly when I tested assigning _epoch to false. Cheers. Also I think the response wasn't JSON because my endpoints hadn't finished deploying to app engine before I set the new version as the default version – Rusty Rob Oct 21 '14 at 07:01
  • I got a very similar error when a REST API unexpectedly returned a blank string rather than the expected JSON object - and code like `result.someProperty = "blah"` thus raised this error. – Ken Smith Nov 29 '14 at 18:36