0
Explanation:
Good guys in my app.js file have many lines of codic ... Some functions ... But I think it doesn’t matter much, so what I want to do is export a variable that I need to use in another already tried file everything else will not, I tried even in the simplest ways.
What I want to do:
//app.js
token = 'Teste' //Variável que quero exportar
module.exports = {
token:token
}
And in the file Account.js
//account.js
x = require("./app.js");
console.log(x.token);
Problem: Guys if I test this comic works, however in my project not only has this, I have other functions and imports of lib ... But it doesn’t work ... It always does Undefined.
Another solution I found:
//app.js
token = 'Teste'
exports.token = token
//account.js
x = require("./app.js")
console.log(x.token)
Anyway, it worked, but why didn’t it work out the way I wanted it to? I do not want to use this way just because it works, even because the way I wanted to do works in a file that contains only this snippet, but in my file there is not only this, it has some more functions ... And I don’t know if it affects the way I want to export the variable.
The scenario where I want to apply the first way I want to do:
async function verify(req,res,username,password){
db.serialize(async function (){
const query = 'SELECT * from Users WHERE User = (?) AND Password = (?)'
db.all(query,[username, password],async function(err,rows){
try{
if(rows.length == 1){
console.time("time1")
await console.log(rows);
console.log("Correct user");
res.render(__dirname + "/View/Home/index.handlebars");
module.exports = {
rows:rows //Desta forma era pra funcionar
}
console.timeEnd("time1")
}
else{
console.log("Incorrect user!")}}
catch{
console.error("\x1b[31m","Problema com a função de autenticação, erro: \n", err);
}
})
})}
But in my file Account.js:
const express = require('express');
const app = express();
const router = express.Router();
const rows = require("./app.js")
const io = require('socket.io')
const handlebars = require("express-handlebars");
const multer = require("multer");
const path = require("path");
app.engine("handlebars", handlebars({defaultLayout:false}));
app.set("view engine","handlebars");
app.get('/',(req,res) => {
res.render(__dirname + "/View/Account/account.handlebars")
console.log(rows.rows);
//Quando eu entrasse nessa rota eu queria que aparecesse o valor da variável da forma que eu queria exportar como havia explicado
});
module.exports = app
Type of error: Undefined
There are more things in my app.js file but I only put the function where I want to use the export module.
There is nothing wrong with any of your codes. They all work correctly (except the quotation marks at the end of
x.token
), but I think it was an accident during the question. Anyway, try to create a [mcve] and bring it here. No need to include whole your file, just try reproduce what is not working (because currently the question is "ok").– Luiz Felipe
Yes it was an accident I will edit, but I also saw that it’s okay when you test only this way but my app.js and Account.js is much bigger than what I showed and maybe it interferes you think I should post all the files' comic here? Because the original comic is big
– user212376
I edited as recommended :)
– user212376
@William you are giving the export inside the Verify function that is not even being called, maybe performing her call at the end of the file works, but you are doing at least something peculiar, calling the export inside an asynchronous function, maybe it would not be better to export the function that returns this data and then use in Account.js?
– João Victor Souza
It is being called only showed where I tried to use, but the problem is that when I call the function of Undefined was not to happen this.
– user212376
And I can’t export function pq I won’t use function only variable.
– user212376