Javascript:无法分配给 false 的只读属性“_epoch”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26413084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Cannot assign to read only property '_epoch' of false
提问by robert king
I have a method to process the response from my google javascript client (gapi):
我有一种方法来处理来自我的 google javascript 客户端 (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?
错误是什么意思?我不应该为响应分配值吗?
回答by itsmejodie
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.
看起来问题在于您的processResponse()
回调实际上被赋予了false
. 因此,本质上您正在尝试分配假值的 _epoch 属性。
请参阅: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.
请求成功或失败时执行的回调函数。jsonResp 包含解析为 JSON 的响应。如果响应不是 JSON,则此字段将为 false。
When you're running javascript in strict mode ('use strict'
), it will raise a TypeError
like you are experiencing:
当您在严格模式 ( 'use strict'
)下运行 javascript 时,它会引发TypeError
您遇到的类似问题:
'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/
JSFiddle:http: //jsfiddle.net/0tq6mobm/
Suggest that you check the value of response before trying to assign your timestamp to the response
.
建议您在尝试将时间戳分配给response
.