-2
Guys, I’m not finding anything related, it’s as follows: I’m learning the MVC model, using the Express module (Nodejs), connection with Mysql Ok and already have a little interaction between the pages on the WEB, all beautiful so far, but has a problem. I have no idea how to display the result of the query personalized in an HTML page, the most I’ve ever been able to display with JSON turning into String and setting on the screen.
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');
}
Within the scope of pool.query(), I get the array I want to display the variable 'vector' :
[ 'Vermelho', 'Preto', 'Vermelho', 'Verde', 'Vermelho', 'Preto' ]
Now when I give a 'console.log(result)', I don’t even know what that means:
<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
}
You must render the page inside the callback of
pool.query
, which returns an object because the SQL query is run asynchronously– Costamilam
I did that and it returned me a pretty crazy error -> Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at Serverresponse.setHeader (_http_outgoing.js:494:11) at Serverresponse.header ...
– Carlos Augusto
And then man, I don’t know what I was doing wrong, but I tried again and it worked out for callback, see.
– Carlos Augusto