Javascript: Unable to assign read-only property _epoch

I have a method for processing a response from my google javascript client (gapi):

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

Several times I got 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 error usually does not occur, so I was not able to register what actually looked like an answer.

What does the error mean? Should I not assign values ​​to the answer?

+6
source share
1 answer

It seems like the problem is that your processResponse() callback is actually getting false . Thus, you are trying to assign the _epoch property to a false value.

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

From the manual:

A callback function that is executed when the request succeeds or fails. jsonResp contains the response processed as JSON. If the response is not JSON, this field will be false .

When you run javascript in strict mode ( 'use strict' ), it will raise a TypeError as you experience:

 '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 checking the meaning of the response before attempting to assign a response timestamp.

+9
source

Source: https://habr.com/ru/post/976844/


All Articles