开发者问题收集

spservices.js 返回此错误:-未捕获(在> promise中)TypeError:无法读取未定义的属性(读取>'LookupList')

2021-12-08
554

在我们的 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_g1bXKUk9rhwfVCtK?e=lny4kw .. 希望这有帮助,谢谢

2个回答

好的,嗯,我对 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()];
The Bomb Squad
2021-12-14

正如 The Bomb Squad 在他们的回答中提到的那样,您似乎正在尝试读取未定义对象的属性。

我的猜测是,也许您在调用 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.")
}
zolastro
2021-12-16