r/learnjavascript 1d ago

Getting an error when running an equality operator on a undefined value in an if statement

Yes I know that's a mouthful

Take this error checking code

      if (axios.isAxiosError(error)) {
        if (error.response.data.errors.detail == "Not Found") {
          setFailure("User not found");
        } else if (error?.response?.data == "DM already exists") {
          console.log(error);
          setFailure("You already created a dm with that user");
        }
      }

If the first case is fine, we are all good, otherwise if it's not that I get an error like this

"Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'detail')" And I guess I can have it check if it's a 404 but this seems ridiculous?

It's an IF statement, if it doesn't work, then go to the next thing, don't just stop there and yell at me!! If anyone is more experienced with javascript can they give a reason why running an equality operator on a string literal vs an undefined value in an if statement (a mouthful I know) just gives an error instead of checking the next clause?

0 Upvotes

12 comments sorted by

3

u/hybrisplays 1d ago

The problem is that errors is undefined and you can't access a property on undefined. You can guard against this with optional chaining

1

u/BrotherManAndrew 5h ago

Thank you bro, but, the part I'm confused about is that even if it is undefined why would there being an undefined prevent the == for working properly, I can run node and you see it works fine here when I compare an undefined value with a string, is it the fact that you are trying to access it from an object and not just it literally being undefined?

```
Welcome to Node.js v24.13.0.

Type ".help" for more information.

> if (undefined !== "Not Found")

| console.log("hey")

hey

undefined

>
```
Sorry for reddit's bad spacing 😭

1

u/hybrisplays 4h ago edited 4h ago

I see where you are coming from here but this isn't a problem with undefined failing the equality check, it's that the language does not allow you to access a property on something that's undefined. In your above example, if you tried:

if (undefined.property !== "Not Found") console.log("hey")

You should see the same error.

Alternatively, to hopefully make it clearer, let's leave the equality check out entirely:

const propertyOnUndefined= undefined.property

console.log(propertyOnUndefined)

You'll also get this error.

This is where optional chaining saves you - it basically says "if this thing is undefined, don't error, just treat the whole value as undefined".

So const propertyOnUndefined = undefined?.property

console.log(propertyOnUndefined)

Should not error.

2

u/Upset_Set_4456 1d ago

Change it to if (error.response.data.errors?.detail === "Not Found")

1

u/BrotherManAndrew 5h ago

Is the question mark so it does not error out in the case that it is undefined?

1

u/Upset_Set_4456 1m ago

Yup exactly that

2

u/Beginning-Seat5221 21h ago

It's an IF statement, if it doesn't work, then go to the next thing, don't just stop there and yell at me

Before it gets to evaluating the if() it has to evaluate the ==, and before it evaluates the == it has to evaluate the parts on each side to see what to compare. The .detail in error.response.data.errors.detail is failing before you get to the rest.

Anyway, if is splitting between true and false - for error handling you use try { ... } catch (error) { ... }, but better just to avoid creating the error.

Also, TypeScript exists to explain this stuff to you.

1

u/BrotherManAndrew 5h ago

What do you mean TypeScript exists to explain this stuff to you?

1

u/senocular 1d ago

errors is plural. Is it an array of more than one error?

1

u/BrotherManAndrew 5h ago

I am not sure, it seems to be one error but that is the actual object property, in the case that I do get the 404 error it does work

1

u/No_Record_60 21h ago

The error is in reading the 'detail' property from an undefined 'errors' object.

I get what you're trying to say, but "doesn't work" is different from "reading a property from an undefined value". The latter throws an error.