spservices.js.Error:- Uncaught (in > promise) TypeError: Cannot read properties of undefined (reading > 'LookupList')

john john 916 Reputation points
2021-12-09T00:03:40.393+00:00

Inside our SPFx web part we have the following code inside the spservices.js code:-

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];  
                }  
            });  
        });  
    };  
  

but on runtime the SPFx web part will return this error and it will keep loading forever:-

calendar-web-part_a87ac4ce95dc9057c9f00ccd9727c133.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'LookupList')    

this is the piece of code which is returning the error:-

return [4 /*yield*/, web.lists.getById(results[0].LookupList)  

as follow:-

156019-image.png

any advice on this please?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,210 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,563 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 29,116 Reputation points Microsoft Vendor
    2021-12-13T05:55:40.527+00:00

    Hi @john john ,
    The LookupList property is undefined because it might doesn't have an initial value. So in your service, try to initialize the LookupList property or confirm the value has pass to the LookupList.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.