Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

66
Views
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
Answer question
Find remote jobs