MVC Web Standard with Nodejs

Asked

Viewed 451 times

-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
}

Source code

  • 1

    You must render the page inside the callback of pool.query, which returns an object because the SQL query is run asynchronously

  • 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 ...

  • And then man, I don’t know what I was doing wrong, but I tried again and it worked out for callback, see.

1 answer

0


buddy, all right?

Using Node.js you cannot display dynamic content within an HTML page. To do this, there is a package called EJS, made for exactly what you need. If you already have an HTML file, simply change the extension to . ejs

1- Install EJS with: npm install ejs --save

2-Configure the server to recognize ejs:

const app = require('express')();
const ejs = require('ejs');

Abaixo você informa para o express que quer usar o EJS
app.set('view engine', 'ejs');

Abaixo você informa onde as views estão
app.set('views', '/views');

3-Changes the . html extension to . ejs

Ready! Now you can already display dynamic content on your page.

Learn more about how EJS works on the NPM website: https://www.npmjs.com/package/ejs

I hope I’ve helped.

  • Oops, I imagined it. By the way, I’m already using the . ejs , but still no idea how to recover the result of an asynchronous function (pool.query) to my front. I’m going to do a little more research on the EJS, I think. VLW for the tip bro.

  • What’s up, bro, blz? I’ve been doing some research, and this is what I really need.

  • Glad I could help. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.