gpt4 book ai didi

Javascript:无法分配给 false 的只读属性 '_epoch'

转载 作者:数据小太阳 更新时间:2023-10-29 05:10:09 25 4
gpt4 key购买 nike

我有一个方法来处理来 self 的 google javascript 客户端 (gapi) 的响应:

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

有几次我遇到了以下错误:

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)

这个错误通常不会发生,所以我没有设法记录响应的实际情况。

错误是什么意思?我不应该为响应赋值吗?

最佳答案

看起来问题在于您的 processResponse() 回调实际上被赋予了 false 值。所以本质上,您是在尝试为 _epoch 属性分配一个错误值。

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

来自手册:

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.

当您在严格模式('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/

建议您在尝试将时间戳分配给 response 之前检查 response 的值。

关于Javascript:无法分配给 false 的只读属性 '_epoch',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26413084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com