繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM