export and use variables in NodeJS

2

I have the following file where the values assigned to two variables are stored; same that I export with module.exports

//archivo datos.js

const num1 = 12
const num2 = 22

module.exports = {
    uno: num1,
    dos: num2
}

Now in a separate file I receive those values that I want to use within a function to be able to add them and return their value

//archivo app.js

let op = require("./datos.js")

let numeroUno = op.uno
let numeroDos = op.dos

console.log(numeroUno + numeroDos)
  

If I do it in the previous way, accessing with the variable op and then a   the desired key, I can do the sum without problems

However if I pass those variables inside a function I get an error of type NaN

//De este modo no me sale el ejercicio

let op = require("./datos.js")

let numeroUno = op.uno
let numeroDos = op.dos

function suma(numeroUno, numeroDos){
    return numeroUno + numeroDos
}

console.log(suma())
  

The console error is NaN

    
asked by element 01.09.2018 в 01:29
source

2 answers

4

The function expects two parameters and you are not passing any. Try it like this:

let numeroUno = 1
let numeroDos = 2

function suma(numeroUno, numeroDos){
    return numeroUno + numeroDos
}

console.log(suma(numeroUno, numeroDos))
    
answered by 01.09.2018 / 01:33
source
2

You need to pass the value of the variables to the function suma

suma(numeroUno, numeroDos)

Apart from that if you agree in this way

let numeroUno = op.uno

You are breaking the principle of encapsulation, which should be through the setter and getters

function getNumeroUno(){
return numeroUno;
}
    
answered by 01.09.2018 в 01:40