![](/img/trans.png)
[英]ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')
[英]spservices.js is returning this error:- Uncaught (in > promise) TypeError: Cannot read properties of undefined (reading > 'LookupList')
在我們的 SPFx SharePoint 在線 web 部分中,我們在 javascript 文件中有以下代碼:-
spservices.prototype.getLookupFieldOptions = function (siteUrl, listId, fieldInternalName) {
return __awaiter(this, void 0, void 0, function () {
var fieldOptions, web, results, options, _i, options_1, option, error_14;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
fieldOptions = [];
_a.label = 1;
case 1:
_a.trys.push([1, 5, , 6]);
web = new Web(siteUrl);
return [4 /*yield*/, web.lists.getById(listId)
.fields.usingCaching()
.filter("InternalName eq '" + fieldInternalName + "'")
.select("LookupList", "LookupWebId", "LookupField")
.top(1)
.get()];
case 2:
results = _a.sent();
if (!results) return [3 /*break*/, 4];
return [4 /*yield*/, web.lists.getById(results[0].LookupList)
.items.usingCaching()
.select("ID", results[0].LookupField)
.getAll()];
case 3:
options = _a.sent();
if (options && options.length > 0) {
for (_i = 0, options_1 = options; _i < options_1.length; _i++) {
option = options_1[_i];
fieldOptions.push({
key: option.ID,
text: option[results[0].LookupField]
});
}
}
_a.label = 4;
case 4: return [3 /*break*/, 6];
case 5:
error_14 = _a.sent();
return [2 /*return*/, Promise.reject(error_14)];
case 6: return [2 /*return*/, fieldOptions];
}
});
});
};
但在運行時 SPFx web 部分將返回此錯誤,它將永遠加載:-
calendar-web-part_a87ac4ce95dc9057c9f00ccd9727c133.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'LookupList')
這是返回錯誤的代碼:-
return [4 /*yield*/, web.lists.getById(results[0].LookupList)
如下:-
請問有什么建議嗎?
我不能在問題中發布整個代碼,因為它會超過字符限制.. 所以我上傳了這個 url @ https://1drv.ms/t/s?At147xVvrdC_g1bXKUnylk9rhf.K.K.K.K .希望這有幫助謝謝
正如炸彈小隊在他們的回答中提到的那樣,您似乎正在嘗試讀取未定義的 object 的屬性。
我的猜測是,在調用results[0].LookupList
時,您可能正在嘗試讀取空數組的第一個元素,因為即使數組為空, if (results){...}
也會計算為true
。 嘗試用它代替if (results.length > 0){...}
。
let results = [] if (results) { console.log("Results is empty, but it still gets evaluated as true.") } if (results.length > 0) { console.log("However, checking its length does the trick.") }
Ok, um idk much about spservices but I do know that kind of error.. so I can assume that the select
function attempts to read nested properties for each value you place and I also know that the value web.lists.getById(listId)
畢竟,當所有這些函數到達select
function 時,所有這些函數都不會以未定義的形式結束,否則錯誤將被Uncaught (in > promise) TypeError: Cannot read properties of undefined (reading > 'select')
有了這個,我會把你的case 1
改成這個
case 1:
_a.trys.push([1, 5, , 6]);
web = new Web(siteUrl);
let someWebListThing=web.lists.getById(listId);
const originalSelect=someWebListThing.select.bind(someWebListThing);
someWebListThing.select=function(){
return originalSelect(...[...arguments].filter(selection=>{
try{originalSelect(selection); return true}
catch{return false} //filters out error causing values so it should work but naturally this is gonna be slower ;-;
}))
}
return [4 /*yield*/, someWebListThing
.fields.usingCaching()
.filter("InternalName eq '" + fieldInternalName + "'")
.select("LookupList", "LookupWebId", "LookupField")
.top(1)
.get()];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.