Jump to content
Fórum Script Brasil
  • 0

Fazer a busca em um banco de dados e exibir uma tabela html com o resultado


Rūkasu. kun

Question

const express = require('express');
const mysql = require('mysql');
const app = express();
const port = 3050; // Porta em que o servidor Express irá rodar

const con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "bd_bt"
});

// Conecta ao banco de dados
con.connect(function(err) {
  if (err) throw err;
  console.log("Conectado ao banco de dados");
});

// Define a rota inicial para exibir os resultados
app.get('/', (req, res) => {
  const cSQL = `select * 
    from tb_bolsas a, tb_forn b
    where a.tb_forn_cod_forn = b.cod_forn;`

  // Realiza a consulta
  con.query(cSQL, function (err, result) {
    if (err) {
      res.status(500).send('Erro na consulta ao banco de dados');
    } else {
      // Chama a função para exibir os resultados em uma tabela
      const html1 = criarTabelaHTML(result);
      const tabelaResultado = document.getElementById('tabelaResultado'); // Obtém o elemento pelo ID
      tabelaResultado = html1; 
      
    }
  });
});

function criarTabelaHTML(result) {
  // Cria a estrutura da tabela HTML
  let html = '<table>';
  // Cabeçalho da tabela
  html += '<tr>';
  for (const field in result[0]) {
    html += `<th>${field}</th>`;
  }
  html += '</tr>';

  // Linhas de dados da tabela
  for (const row of result) {
    html += '<tr>';
    for (const field in row) {
      html += `<td>${row[field]}</td>`;
    }
    html += '</tr>';
  }

  html += '</table>';
  return html;
}

app.listen(port, () => {
  console.log(`Servidor Express está rodando na porta ${port}`);
});

Esse codigo continua dando o seguinte erro toda vez que tento entrar em "http://localhost:3050/": 

Servidor Express está rodando na porta 3050
Conectado ao banco de dados
c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

ReferenceError: document is not defined
    at Query.<anonymous> (c:\Users\User\Desktop\bd_brechotendenze\main.js:33:31)
    at Query.<anonymous> (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\Connection.js:526:10)
    at Query._callback (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\Connection.js:488:16)
    at Sequence.end (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
    at Query._handleFinalResultPacket (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)
    at Query.EofPacket (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\sequences\Query.js:133:8)
    at Protocol._parsePacket (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (c:\Users\User\Desktop\bd_brechotendenze\node_modules\mysql\lib\protocol\Protocol.js:38:16)
 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      651.9k
×
×
  • Create New...