exports.exibirCarros = (req, res) => {
let query = 'SELECT * FROM carros';
let result = pool.query(query, (err, rows) => {
if(err) console.log(err);
let vetor = [];
// vetor.push(JSON.stringify(rows));
rows.map((todo) => vetor.push(todo.cor));
// console.log(vetor);
return vetor;
});
console.log(result);
res.render('pages/showCars');
}
Dentro do escopo do 'pool.query()', eu consigo o array que eu quero exibindo a variável 'vetor' :
[ 'Vermelho', 'Preto', 'Vermelho', 'Verde', 'Vermelho', 'Preto' ]
Agora quando dou um 'console.log(result)', eu nem sei o que significa isso:
<ref *1> Query {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_callback: [Function (anonymous)],
_callSite: Error
at Pool.query (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/mysql/lib/Pool.js:199:23)
at exports.exibirCarros (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/controllers/carroController.js:14:23)
at Layer.handle [as handle_request] (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/layer.js:95:5)
at next (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/layer.js:95:5)
at /home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:335:12)
at next (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/carlos/Documentos/JavaScript/example_MVC_FullWeb/node_modules/express/lib/router/index.js:174:3),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular *1], _timeout: null },
sql: 'SELECT * FROM carros',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null
}