1

I have a question about NodeJs. Since 6.6 it say when unhandled promise can't be fetched.

To avoid that, I red I need to catch the error, but... If I got that, how should I proceed?

args[1] = args[1].split('!')[1].split('>')[0]

I tried to add .catch(err => console.log(err)) but nothing seems to change.

Thanks in advance! :)

alexmac
  • 19,087
  • 7
  • 58
  • 69
Izio
  • 378
  • 6
  • 15

3 Answers3

3

Instead of catching, you can avoid an error by substituting a string when the first split produces no second index.

args[1] = (args[1].split('!')[1] || "").split('>')[0];

This assumes that args[1] is definitely present. If that's not certain, you can do a similar substitution.

args[1] = ((args[1] || "").split('!')[1] || "").split('>')[0];

And finally, you can provide a default value in case any part of that failed to produce something useful.

args[1] = ((args[1] || "").split('!')[1] || "").split('>')[0] || "DEFAULT";
spanky
  • 2,768
  • 8
  • 9
0

.catch(err => console.log(err)) is used to catch errors from promises. Use the standard form instead:

try{
    args[1] = args[1].split('!')[1].split('>')[0]
}
catch(err){
    console.log(err)
}
Valera
  • 2,665
  • 2
  • 16
  • 33
0

More or less this is not promising. it's just a simple javascript statement which can be captured directly as below: Try using try catch on the whole

try{
   args[1] = args[1].split('!')[1].split('>')[0]
}catch(e){
console.log(e); 
// Most possible error would be TypeError: Cannot read property 'split' of undefined
at <anonymous>:1:23
}
squiroid
  • 13,809
  • 6
  • 47
  • 67