Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

70
Vistas
Set value of a js file properties from outside of it

I`m new to JS and apologize for asking a primary question! We have this first.js file as a js class example with:

const {MakeRequest} = require("./Request");

let api;
let token;
let unique_token;

function chat(data, unique_token = null) {
    try {
        if (api != null && token != null) {
            return MakeRequest(token, api, data, unique_token)
        } else {
            return {
                "provider": {
                    "website": "https://example.com",
                    "source": "Example"
                },
                "status": true,
                "response": [],
                "options": {},
                "error": {
                    "code": 207,
                    "message": "Token or Api token did not valueted!"
                }
            }
        }
    } catch (e) {
        return {
            "provider": {
                "website": "https://example.com",
                "source": "Example"
            },
            "status": true,
            "response": [],
            "options": {},
            "error": {
                "code": e.code,
                "message": e.message
            }
        }
    }
}

module.exports = {
    token,api,unique_token,chat
}

also, I have this second.js file as a executable js file:

const object = require("./first.js")

object.token ="123456"
object.api ="123456"
object.token ="123456"
console.log(object.chat("hello"))

If I run the second.js file, the api variable is undefined and didn`t get the value from second.js, how can I resolve this problem without change the second.js codes!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't edit a variable from a scope outside where it was declared.

Consider passing the values as arguments to chat function when you call it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda