how to make javascript take an n number of elements from a database

Asked

Viewed 150 times

0

I would like to take for example all the names of a database table and save in an array(or matrix) and then list in an html table

how can I create a new method

BS: I have that part ready:

var mysql = require('mysql');
class Dao {
  constructor() {

  }
  conectaBanco() {
    var con = mysql.createConnection({
      host: "localhost",
      port: "3307",
      user: "root",
      password: "usbw",
      database: "amor"
    });
    return con;
  }
  executaSql(sql) {
    var conector = this.conectaBanco();
    conector.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
      conector.query(sql, function(err, result) {
        if (err) throw err;
        console.log(result);
      });
      conector.end();
    });
  }
}

var dao = new Dao();
dao.executaSql("select * from nomes");
module.exports = new Dao();

  • But the result inside the callback is already an array.

  • What gives you that console.log(result);?

No answers

Browser other questions tagged

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