I’m experiencing an issue while trying to upload my TypeScript NestJS application to AWS as an API.This API working before but after update get this error I encountered the following error in the AWS logs:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘0’ of undefined.
how to troubleshoot and fix this error? I’m a beginner, so any guidance would be greatly appreciated.
error from aws web.stdout.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=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 61)
Sep 9 09:05:57 ip-172-31-32-56 web: (node:25450) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
the code from resource.cache
@Injectable()
export class ResourceCacheService implements OnModuleInit {
private readonly logger = new Logger(ResourceCacheService.name);
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
@InjectModel("resource") private readonly model: Model<Resource, ResourceKey>,
private readonly infoService: InfoCache,
private readonly categoryService: CategoryCacheService
) {}
public async find(type: ResourceType, subType: string): Promise<Resource[]> {
return await this.cacheManager.get(this.getKey(type, subType));
}
public async getAlf() {
return await this.cacheManager.get(this.getKeyPublic(ALF));
}
public async getContact() {
return await this.cacheManager.get(this.getKeyPublic(CONTACT));
}
public async getHomepage() {
return await this.cacheManager.get(this.getKeyPublic(HOMEPAGE));
}
public async getManagement() {
return await this.cacheManager.get(this.getKeyPublic(MANAGEMENT));
}
public async getMembership() {
return await this.cacheManager.get(this.getKeyPublic(MEMBERSHIP));
}
public async getStudyAndConsultation() {
return await this.cacheManager.get(this.getKeyPublic(STUDY_AND_CONSULTATION));
}
public async getTraining() {
return await this.cacheManager.get(this.getKeyPublic(TRAINING));
}
public async getHistory() {
return await this.cacheManager.get(this.getKeyPublic(HISTORY));
}
public async getVisionMission() {
return await this.cacheManager.get(this.getKeyPublic(VISION_MISSION));
}
public async getLoginPage() {
return await this.cacheManager.get(this.getKeyPublic(LOGIN));
}
public async onModuleInit() {
await this.refreshCacheInternal();
await this.refreshCachePublicFacing();
}
@Interval(CACHE_REFRESH_INTERVAL())
public refreshCache() {
this.refreshCacheInternal();
this.refreshCachePublicFacing();
}
private async refreshCachePublicFacing() {
await this.refreshAlf();
await this.refreshContact();
await this.refreshHistory();
await this.refreshHomepage();
await this.refreshManagement();
await this.refreshMembership();
await this.refreshStudyAndConsultation();
await this.refreshTraining();
await this.refreshVisionMission();
await this.refreshLoginPage();
}
private async refreshCacheInternal() {
let start = moment();
this.logger.log(`Start refresh cache`);
for (let type of ResourceTypeArray) {
let dataList = await this.model.query("type").eq(type).exec();
let subTypeList = dataList.map((e) => e.subType);
for (let subType of subTypeList) {
let dataSubType = await dataList.filter((e) => e.subType == subType);
await this.cacheManager.set(this.getKey(type, subType), dataSubType);
}
}
this.logger.log(`Finish refresh cache ${moment().diff(start, "millisecond")}ms`);
}
private getKey(type: any, subType: any) {
return `RESOURCE.${type}.${subType}`;
}
private getKeyPublic(type: any) {
return `RESOURCE.PUBLIC.${type}`;
}
private async refreshContact() {
let type = ResourceType.Contact;
let subType = ResourceSubType.Contact;
let cachedData = {
address: (await this.find(type, subType.Address))[0].content,
email: (await this.find(type, subType.Email))[0].content,
fax: (await this.find(type, subType.Fax))[0].content,
phone: (await this.find(type, subType.Phone))[0].content,
companyCatalog: (await this.find(type, subType.CompanyCatalog))[0].url,
socialMedia: (await this.find(type, subType.SocialMedia)).map((e) => {
return {
platformName: e.title,
accountName: e.content,
};
}),
};
this.cacheManager.set(this.getKeyPublic(CONTACT), cachedData);
}
private async refreshHomepage() {
let type = ResourceType.Homepage;
let subType = ResourceSubType.Homepage;
let carrousel = await this.find(type, subType.Carousel);
// let newProduct = await this.find(type, subType.NewProduct);
let ourStrength = await this.find(type, subType.OurStrength);
let ourFocus = await this.find(type, subType.OurFocus);
let testimoni = await this.find(type, subType.Testimoni);
let cachedData = {
banner: (await this.find(type, subType.Banner))[0].url,
carousel: carrousel
? await Promise.all(
carrousel?.map(async (e) => {
let originInfo: InfoCacheById = (await this.infoService.findById(e.otherData.originId)) as InfoCacheById;
return {
title: e.title,
content: e.content,
imageUrl: originInfo.headerUrl,
linkTo: originInfo.queryKey,
section: e.otherData.section,
};
})
)
: [],
ourStrength: ourStrength?.map((e) => {
return {
title: e.title,
content: e.content,
icon: e.url,
};
}),
ourFocus: ourFocus?.map((e) => {
return {
title: e.title,
content: e.content,
icon: e.url,
};
}),
testimoni: testimoni?.map((e) => {
return {
name: e.title,
position: e.otherData.position,
content: e.content,
};
}),
// newProduct: newProduct?.map((e) => {
// return {
// name: e.title,
// linkTo: e.otherData.linkTo,
// content: e.content,
// };
// }),
};
this.cacheManager.set(this.getKeyPublic(HOMEPAGE), cachedData);
}
private async refreshHistory() {
let type = ResourceType.AboutUs;
let subType = ResourceSubType.AboutUs;
let cachedData = {
history: (await this.find(type, subType.History))[0].content,
historyImage: (await this.find(type, subType.History))[0].url,
founders: (await this.find(type, subType.HistoryFounder)).map((e) => {
return {
name: e.title,
position: e.content,
photo: e.url,
};
}),
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.History),
};
this.cacheManager.set(this.getKeyPublic(HISTORY), cachedData);
}
async refreshVisionMission() {
let type = ResourceType.AboutUs;
let subType = ResourceSubType.AboutUs;
let cachedData = {
vision: (await this.find(type, subType.Vision))[0].content,
mission: (await this.find(type, subType.Mission)).map((e) => {
return {
content: e.content,
icon: e.url,
};
}),
colorPhilosophy: {
image: (await this.find(type, subType.LogoColorPhilosophyImage))?.[0].url,
description: (await this.find(type, subType.LogoColorPhilosophyDescription))?.[0].content,
detail: (await this.find(type, subType.LogoColorPhilosophyDetailDescription))?.map((e) => {
return {
text: e.title,
color: e.content,
};
}),
},
symbolPhilosophy: {
image: (await this.find(type, subType.LogoSymbolPhilosophyImage))?.[0].url,
description: (await this.find(type, subType.LogoSymbolPhilosophyDescription))?.[0].content,
detail: (await this.find(type, subType.LogoSymbolPhilosophyDetailDescription))?.map((e) => {
return {
text: e.title,
color: e.content,
};
}),
},
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.VisionMission),
};
this.cacheManager.set(this.getKeyPublic(VISION_MISSION), cachedData);
}
private async refreshManagement() {
let type = ResourceType.AboutUs;
let subType = ResourceSubType.AboutUs;
let sortedManagement = (await this.find(type, subType.Management)).sort(CommonUtil.dynamicSort("createdAt"));
let cachedData = {
management: (await sortedManagement).map((e) => {
return {
name: e.title,
content: e.content,
photo: e.url,
position: e.otherData.position,
};
}),
};
this.cacheManager.set(this.getKeyPublic(MANAGEMENT), cachedData);
}
private async refreshAlf() {
let type = ResourceType.AboutUs;
let subType = ResourceSubType.AboutUs;
let cachedData = {
description: (await this.find(type, subType.AstagatraLeadersForumDesc))[0].content,
astagatraLeadersForum: (await this.find(type, subType.AstagatraLeadersForum)).map((e) => {
return {
name: e.title,
content: e.content,
photo: e.url,
position: e.otherData.position,
};
}),
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.AstagatraLeadersForum),
};
this.cacheManager.set(this.getKeyPublic(ALF), cachedData);
}
private async refreshMembership() {
let type = ResourceType.AboutUs;
let subType = ResourceSubType.AboutUs;
let cachedData = {
description: (await this.find(type, subType.MembershipDesc))[0].content,
benefit: (await this.find(type, subType.MembershipBenefit))[0].content,
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.Membership),
};
this.cacheManager.set(this.getKeyPublic(MEMBERSHIP), cachedData);
}
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);
}
private async refreshTraining() {
let type = ResourceType.Service;
let subType = ResourceSubType.Service;
let cachedData = {
catalog: (await this.find(type, subType.Training))[0].url,
mainPicture: (await this.find(type, subType.TrainingMainPicture))[0].url,
mainDesc: (await this.find(type, subType.TrainingMainDesc))[0].content,
item: (await this.find(type, subType.TrainingItem)).map((e) => {
return {
content: e.content,
title: e.title,
};
}),
method: (await this.find(type, subType.TrainingMethod)).map((e) => {
return {
url: e.url,
title: e.title,
};
}),
quickLinks: await this.categoryService.findByPageName(QuickLinkPageName.Training),
};
this.cacheManager.set(this.getKeyPublic(TRAINING), cachedData);
}
private async refreshLoginPage() {
let type = ResourceType.Login;
let subType = ResourceSubType.Login;
let cachedData = {
banner: (await this.find(type, subType.LoginImage))?.[0].url,
};
this.cacheManager.set(this.getKeyPublic(LOGIN), cachedData);
}
}
3