Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

65
Vistas
exports variable didn't change after operate by required

I store variables in config.js, and I import those variables to different function with different file.
Then I import those functions and variables to main app.js.
The issue is when I exports as obj, values " could be changed " after call those functions
But I exports as variables directly, values " couldn't be changed " after call those functions

how can I exports variables directly ??

Here is an example below

//config.js
exports.varNum = 100;
exports.varBoolen = false;
exports.obj = {
    name:"Kai",
    age:31,
    single:true
};
// funcA.js
let { varNum, obj } = require("./config")

module.exports = function add (){
    varNum += 1;
    obj.age += 1;
}
//funcB.js
let { varBoolen, obj } = require("./config")

module.exports = function change (){
    varBoolen = !varBoolen;
    obj.single = !obj.single;
}

//app.js
const express = require("express");
const app = express();
let { varNum, varBoolen, obj } = require("./config")
let add=require("./funcA")
let change=require("./funcB")

app.get("/", (req, res) => {
    res.json({ status: "ok", data: { varNum, varBoolen, obj } })
})
app.get("/add", (req, res) => {
    add();
    res.json({ status: "add" })
})
app.get("/change", (req, res) => {
    change();
    res.json({ status: "change" })
})
7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.