gpt4 book ai didi

javascript - 无法读取未定义的属性 '0'(仅在单元测试中)

转载 作者:行者123 更新时间:2023-11-27 23:36:51 26 4
gpt4 key购买 nike

我不明白为什么会出现以下错误:

Chrome 43.0.2357 (Windows 7 0.0.0) Requests controller test should fill properties from result from xhr requests FAILED TypeError: Cannot read property '0' of undefined at getLastStatus (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/app/tools.js:68:43) at C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/app/requests/requests.js:238:42 at processQueue (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:14792:28) at C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:14808:27 at Scope.$eval (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:16052:28) at Scope.$digest (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:15870:31) at Scope.$apply (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:16160:24) at done (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:10589:47) at handleResponse (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular-mocks/angular-mocks.js:1194:9) at Function.$httpBackend.flush (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular-mocks/angular-mocks.js:1553:26)

此错误仅显示在 karma 日志上。

但它不应该因为循环条件而未定义。

在tools.js文件中:

function getLastStatus(statuses) {
var lastStatusDate = Date.parse(statuses[0].date); // this is the line pointed in the error
var lastIndex = 0;

for (var i = 0; statuses[i]; i++) {
var currentStatusDate = Date.parse(statuses[i].date);

if (currentStatusDate > lastStatusDate) {
lastStatusDate = currentStatusDate;
lastIndex = i;
}
}
return statuses[lastIndex];
}

在 requests.js 文件中:

Requests.query().$promise.then(function(result) {
controllerScope.requestsList = [];
for (var i = 0; result[i]; i++) {
var currentRequest = {
id: result[i]["id"],
projectTitle: result[i]["project"].title,
status: getLastStatus(result[i]["statuses"]).status, // this is the line pointed in the error
description: result[i]["description"],
regions: regionsIdToName(controllerScope.regionsList, result[i]["project"].regions),
requestDate: moment(result[i]["request_date"]).format("MM-DD-YYYY"),
developers: result[i]["developers"],
statusDate: moment(getLastStatus(result[i]["statuses"]).date).format("MM-DD-YYYY")
};
controllerScope.requestsList.push(currentRequest);
}
});

编辑:getLastStatus(statuses) 函数的第一行添加 console.log(statuses); 时:

一个对象数组显示在浏览器控制台上,就像它应该的那样。

但是Karma控制台上显示undefined。所以我不明白是什么原因造成的。

最佳答案

result[i] 不能未定义(由于循环检查)。但似乎 result[i]["statuses"] 未定义。您将 undefined 传递给 getLastStatus,然后尝试获取它的“0”元素。您应该检查 result[i]["statuses"] 是否未定义。

关于javascript - 无法读取未定义的属性 '0'(仅在单元测试中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066621/

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