-1
I’m having a pertinent question, I’m starting a project in Lectron where I have to make the connection with two different banks, one location and the other in clouds, in my Node js code, I have two files the index.js where I put the data referring to Electron and another database.js where I will call the database, hence the doubt, when using the module.Exports = Function ..., to export the function to index.js, I cannot use this sporty function to make an if condition where depending on the answer of the first sql I must run the second in another database, follow the code to better understand
database js.
var mysql = require('mysql');
module.exports = function execSQLbd(sqlQry){
var connection = mysql.createConnection({
host : 'localhost',
port : 3306,
user : 'teste',
password : 'teste',
database : 'teste'
});
connection.connect(function(err) {
// in case of error
if(err){
console.log(err.code);
console.log(err.fatal);
}
});
connection.query(sqlQry, function(error, rows, fields){
if(error){
console.log("An error ocurred performing the query.");
console.log(err);
return;
}
nome = rows[0].nome;
id = rows[0].id;
connection.end();
return id;
//console.log(id);
});
}
When I change the Return to console.log it appears the result of the database, but the problem is to put this result in if, because when I put the condition in index.js it first runs if and then the database function; Thanks in advance to those who can collaborate...
index js.
var execSQLbd = require('./models/db1');
banco1 = new execSQLbd("SELECT * FROM pagamentos WHERE id = '1'");
if (banco1 == 1)
return true;
const {app, BrowserWindow} = require ('electron');
let mainWindow;
app.on('ready', () =>{
mainWindow = new BrowserWindow ({
});
mainWindow.loadURL(`file://${__dirname}/teste.html`)
});
Because you are using
new
to invoke a function? (new execSQLbd()
)– Allan Juan
Regardless of using new, I can’t return the value to use in an if;
– Ismael Melkis