본문 바로가기
Log/Trouble shoot

500 Error∵accessing deconstructed value(undefined)

by RIEM 2023. 9. 22.
728x90

Situation

  • Frontend team reported 500 error on production server which is internal server-related error

Cause

  • the property deconstruction error
  • It is caused because code tried to deconstruct undefined property.
{
    "statusCode": 500,
    "status": "TypeError",
    "message": "Cannot destructure property 'appName' of 'appDict.get(...)' as it is undefined."
}
async getUser(getUserDto: GetUserDto): Promise<object> {
    ...    
    const { appName, platform }: Partial<queryData> = appDict.get(app['appId']);

...

Solution

// adding if condition before accessing
if (appDict.get(app['appId'])) {
  • Put if statement to make sure it is not undefined
  • If is undefined, deconstructing assignment will not be proceeded and there will be no error

Today I learn

Consider variables when it is undefined

  • Whenver I handle variables, I should consider possibilities of variables containing undefined. Before some jobs dealing with variables such as deconstructing assignment, make sure the variable contain the expected value

Dive as soon as possible when production down

  • As soon as error occurs on production server, our engineering team dive into this problem to fix right away. It was my issues but team tried solving one problem together to reduce downtime of production server
728x90

댓글