Hello, I'm developing a framework to start developing on node, I'm a beginner so forgive me anything.
indexModel.js
var Model = require('./model.js');
var model = new Model();
var data;
class IndexModel {
// Função inicial
constructor() {
}
// Data banco
banco() {
return model.Query('SELECT * FROM test WHERE id = ?', [2], 'select');
}
// Retorne a data;
data() {
return data;
}
}
model.js:
var mysql = require('mysql');
var data;
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "nodejs",
timezone: "-3:00"
});
class ModelModel {
// Função inicial
constructor() {
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
}
// Retorna o valor da query passada
Query(query, array, type) {
con.query(query, array, function (error, results, fields) {
if (error) throw error;
switch (type) {
case 'select':
data = results;
break;
case 'insert':
data = results.insertId;
break;
case 'update':
data = 'changed ' + results.changedRows + ' rows';
break;
case 'delete':
data = 'deleted ' + results.affectedRows + ' rows';
break;
default:
data = results;
break;
}
});
return data;
}
ReturnQuer(result) {
data = result;
console.log('aqui'+data);
}
}
I need to get the date value out of the function, but I always get 'undefined', inside the function I'm getting everything correctly, I would like to know if there is a way to get the return of the query result, or if I'm doing something wrong.
NOTE: I removed 'use strict' and module.exports because for some reason I can not add it in the stackoverflow text tool as a code.