0

One annoying thing about writing js app is that nested object can caused error and break down the entire app.

if(result.applicant._id === null || applicant_id !== result.applicant._id.toString()){
console.log('redirect user');
}

Given above code, it can be dangerous, what if result.applicant._id is null? then toString will be invalid because it can't be undefined.toString(). How to ensure toString() work in this case? I can do

if(result.applicant._id === null || applicant_id !== (result.applicant._id && result.applicant._id.toString())){}

but that's so unclean. I found that I'll have many duplication just for the sake of checking something exist using js.

Jamie Aden
  • 943
  • 2
  • 12
  • 20
  • Have you tried using `try{}catch(){}` expressions? – Luis felipe De jesus Munoz Mar 14 '18 at 15:14
  • Possible duplicate of [What's the best way to convert a number to a string in JavaScript?](https://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string-in-javascript) – Zenoo Mar 14 '18 at 15:14
  • 2
    Possible duplicate of [How to avoid 'cannot read property of undefined' errors?](https://stackoverflow.com/questions/14782232/how-to-avoid-cannot-read-property-of-undefined-errors) – str Mar 14 '18 at 15:17

3 Answers3

1

Your version works without ever hitting undefined.toString() because the if condition will be short-circuited (short-circuitted?) as result.applicant._id === null would evaluate as true and never evaluate applicant_id !== result.applicant._id.toString().

The test is already there no need to add extra checks in this case.

Update Just realised the === will not match undefined.

Just change the first part to result.applicant._id == null which will match undefined and null.

if (result.applicant._id == null || applicant_id !== result.applicant._id.toString()){
    console.log('redirect user');
}

I know you may end with linting warnings but in this case that's exactly what you want.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • Updated answer :) – phuzi Mar 14 '18 at 15:27
  • I know linters will complain about using `==` but it is still valid to do it and is "the" way to do this. I only use `==` when needed and tend to leave a comment that that is what is intended in case someone else comes along and decides they want to kill the warning. – phuzi Mar 16 '18 at 14:58
  • @JamieAden the same mater in ths case: "if applicant doesn't exist this will thrown an error too" – sultan Mar 17 '18 at 09:56
0

A bit of short-handing might give you the cleaner code you're looking for. As long as 0 is not a valid application id:

const id = result.application._id || 0;
if(!id || applicant_id !== id.toString()){
  // redirect
}

Edit: To explain - the || in the variable declaration assigns the value to the first truthy value - or the second. Therefore, if the value is undefined, it will fall to the second value (0) which is still falsey and will fail the check, but can still have .toString() called on it.

Edit 2: If your ids are just numbers (which is what it looks like) then you actually don't need to convert it to a string - just let JavaScript's coercion to the work for you by using != instead of !==

const id = result.application._id || 0;
if(!id || applicant_id != id){
 // redirect
}

In JavaScript, 12 == '12' is true. 12 === '12' is false. Generally the advice is to use === unless you consciously want to take advantage of coercion, and this seems like a good case for it :)

veratti
  • 560
  • 3
  • 10
  • using `==` is not safe and I want to avoid that. – Jamie Aden Mar 16 '18 at 14:56
  • It's not unsafe as long as you understand the difference in how it works compared to `===`. It actually serves a pretty specific purpose in JavaScript but it can be confused by programmers coming from other languages where `==` is the default operator. Coercion is a feature of JavaScript, as long as we use it where it makes sense and is intentional :) – veratti Mar 16 '18 at 21:05
0

The shortest way could be this:

.: UPDATED :.

const applicant = result.applicant || {}
if (applicant_id !== `${applicant._id}`) {
  console.log('redirect user');
}
sultan
  • 4,543
  • 2
  • 24
  • 32