Joh*_*ohn 2 javascript sharepoint spservices spfx
在我们的 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];
}
});
});
};
Run Code Online (Sandbox Code Playgroud)
但在运行时,SPFx Web 部件将返回此错误,并且它将永远保持加载:-
calendar-web-part_a87ac4ce95dc9057c9f00ccd9727c133.js:1 未捕获(承诺中)TypeError:无法读取未定义的属性(读取“LookupList”)
这是返回错误的代码:-
return [4 /*yield*/, web.lists.getById(results[0].LookupList)
Run Code Online (Sandbox Code Playgroud)
如下:-
请问对此有什么建议吗?
我无法在问题中发布整个代码,因为它将超出字符限制..所以我上传此网址内的类型脚本文件@ https://1drv.ms/t/s!At147xVvrdC_g1bXKUk9rhwfVCtK?e=lny4kw ..希望这有帮助,谢谢
好的,嗯,我对 spservices 很了解,但我确实知道这种错误..所以我可以假设该select函数尝试读取您放置的每个值的嵌套属性,并且我也知道所有这些函数之后的值web.lists.getById(listId)不会结束当它到达函数时为未定义,select否则错误将是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()];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
516 次 |
| 最近记录: |