I’m encountering a problem while uploading my application to AWS as an API. The AWS logs show the following error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘0’ of undefined.
How can i bypass this type of error?
A step-by-step guide would be greatly appreciated.
error log
Sep 9 09:05:57 ip-172-31-32-56 web: (node:25450) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
Sep 9 09:05:57 ip-172-31-32-56 web: at ResourceCacheService.refreshStudyAndConsultation (/var/app/current/src/resource/service/resource.cache.ts:314:97)
Sep 9 09:05:57 ip-172-31-32-56 web: at runMicrotasks (<anonymous>)
Sep 9 09:05:57 ip-172-31-32-56 web: at processTicksAndRejections (internal/process/task_queues.js:95:5)
Sep 9 09:05:57 ip-172-31-32-56 web: at ResourceCacheService.refreshCachePublicFacing (/var/app/current/src/resource/service/resource.cache.ts:98:5)
Sep 9 09:05:57 ip-172-31-32-56 web: at ResourceCacheService.onModuleInit (/var/app/current/src/resource/service/resource.cache.ts:82:5)
Sep 9 09:05:57 ip-172-31-32-56 web: at async Promise.all (index 0)
Sep 9 09:05:57 ip-172-31-32-56 web: at Object.callModuleInitHook (/var/app/current/node_modules/@nestjs/core/hooks/on-module-init.hook.js:43:5)
Sep 9 09:05:57 ip-172-31-32-56 web: at NestApplication.callInitHook (/var/app/current/node_modules/@nestjs/core/nest-application-context.js:169:13)
Sep 9 09:05:57 ip-172-31-32-56 web: at NestApplication.init (/var/app/current/node_modules/@nestjs/core/nest-application.js:97:9)
Sep 9 09:05:57 ip-172-31-32-56 web: at NestApplication.listen (/var/app/current/node_modules/@nestjs/core/nest-application.js:156:33)
Sep 9 09:05:57 ip-172-31-32-56 web: (node:25450) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=stri`ct` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 61)
the code
private async refreshStudyAndConsultation() {
let type = ResourceType.Service;
let subType = ResourceSubType.Service;
let advisoryBenefit = (await this.find(type, subType.StudyAndConsultationAdvisoryBenefit))[0];
let strategicBenefit = (await this.find(type, subType.StudyAndConsultationStrategicBenefit))[0];
let cachedData = {
catalog: (await this.find(type, subType.StudyAndConsultation))[0].url,
mainPicture: (await this.find(type, subType.StudyAndConsultationMainPicture))[0].url,
catalogMainPicture:(await this.find(type, subType.StudyAndConsultationCatalogMainPicture))[0].url,
mainDesc: (await this.find(type, subType.StudyAndConsultationMainDesc))[0].content,
whyUsDesc: (await this.find(type, subType.StudyAndConsultationWhyUsDesc))[0].content,
methodDesc: (await this.find(type, subType.StudyAndConsultationMethodDesc))[0].content,
methodItem: (await this.find(type, subType.StudyAndConsultationMethodItem)).map((e) => {
return {
content: e.content,
title: e.title,
};
}),
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.StudyAndConsultation),
advisoryBenefit: {
content: advisoryBenefit.content,
icon: advisoryBenefit.url,
},
advisoryDesc: (await this.find(type, subType.StudyAndConsultationAdvisoryDesc))[0].content,
advisoryItem: (await this.find(type, subType.StudyAndConsultationAdvisoryItem)).map((e) => {
return {
content: e.content,
title: e.title,
};
}),
strategicBenefit: {
content: strategicBenefit.content,
icon: strategicBenefit.url,
},
strategicDesc: (await this.find(type, subType.StudyAndConsultationStrategicDesc))[0].content,
strategicItem: (await this.find(type, subType.StudyAndConsultationStrategicItem)).map((e) => {
return {
content: e.content,
title: e.title,
};
}),
};
this.cacheManager.set(this.getKeyPublic(STUDY_AND_CONSULTATION), cachedData);
}
I’m feeling stuck and not sure how to move forward. Could you please offer some assistance?
1
it might be that the this.find(type, subType.StudyAndConsultation)) is undefined, so addeing the “?.” null check will solve this
replace this.find(type, subType.StudyAndConsultation))[0].url
in this format this.find(type, subType.StudyAndConsultation))?.[0]?.url