Stop JSON reading in node.js

0

I'm doing a Web Service in Node.js where I have to do several validations but if one of those validations gives an error I need the Web Service to continue running but stop validating, as if it were a Try ... catch alone I do not know if in my case you can use try ... catch. HELP!

This is the example code:

var number = "No soy un numero";
if(Number.IsInteger(number) == true){
  console.log("Soy un numero")
}else{
   new throw Exception("Se necesita un numero")
 //aqui no deberia de seguir ejecutando el codigo.
}

//No deberia imprimir esto.
console.log("Continua...")
    
asked by java005 02.09.2018 в 20:56
source

2 answers

0

You were very close but you had some syntax errors:

var number = "No soy un numero";
if(Number.isInteger(number) == true){
  console.log("Soy un numero")
}else{
    throw new Error("Se necesita un numero")
 //aqui no deberia de seguir ejecutando el codigo.
}

//No deberia imprimir esto.
console.log("Continua...")

The first one is isInteger starts with lowercase.

The second to execute your error you must use throw new Error in that order and with Error instead of Exception

And about your question if you can use try catch, if you can.

    
answered by 03.09.2018 / 00:50
source
0

Number.isInteger returns true or false so you do not need the check with true, on the other hand you can use the ternary operator as indicated below.

let number = "No soy un numero";
Number.isInteger(number)
  ? console.log("Soy un numero")
  : throw new Error("Se necesita un numero")
    
answered by 04.09.2018 в 17:11