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!
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.