0

I am trying to create a script using Javascript that fetches data from a MySQL database in order to create an array with that data, but I am stuck while trying to make Javascript connect to the DB.

Here is my JS code:

var mysql = require('mysql');

var con = mysql.createConnection({
    host: "*DELETED*",
    user: "*DELETED*",
    password: "*DELETED*",
    database: "*DELETED*"
});

// Array approach
var config = {
        container: "#collapsable-example",

        animateOnInit: true,

        node: {
            collapsable: true
        },
        animation: {
            nodeAnimation: "easeOutBounce",
            nodeSpeed: 700,
            connectorsAnimation: "bounce",
            connectorsSpeed: 700
        }
    };


    var array = [];

for (var i = 0; i <= con.query("SELECT MAX (id_dim) FROM dim"); i++) {

    var nome = con.query("SELECT itempt_dim FROM dim WHERE id_dim = " + i);
    var parente = "parent: " + con.query("Select itempt_dim from dim, rdim Where iddim_rdim = " + i);
    var nomeEscrito = "text: " + con.query("SELECT itempt_dim FROM dim WHERE id_dim = " + i);
    //var droplevel = "childrenDropLevel: " +
    //var pseudo = "pseudo: " +
    //var imagem = "image: " +
    array.push(nome = {
        parente,
        nomeEscrito
    });

}

console.log(array);

chart_config = [config, Sharan, Sharan2];

and I get the following error:

ReferenceError: require is not defined

EDIT1: so i used the JS inside of the PHP file and tried the following:

<?php
      require('common/db.php');
    ?>

    <script type="text/javascript">

      // Array approach
      var config = {
              container: "#collapsable-example",

              animateOnInit: true,

              node: {
                  collapsable: true
              },
              animation: {
                  nodeAnimation: "easeOutBounce",
                  nodeSpeed: 700,
                  connectorsAnimation: "bounce",
                  connectorsSpeed: 700
              }
          };


          var array = [];
          var maxID = "<?php $con->query('SELECT MAX(id_dim) FROM dim'); ?>"

          for (var i = 1; i <= maxID ; i++) {

            var nome = "<?php $con->query('SELECT itempt_dim FROM dim WHERE id_dim = " + i + "'); ?>";
            var parente = "parent: " + " <?php $con->query('SELECT itempt_dim FROM dim, rdim WHERE iddim_rdim = " + i + "'); ?>";
            var nomeEscrito = "text: " + "<?php $con->query('SELECT itempt_dim FROM dim WHERE id_dim = " + i + "'); ?>";
            //var droplevel = "childrenDropLevel: " +
            //var pseudo = "pseudo: " +
            //var imagem = "image: " +
            array.push(nome = {
              parente,
              nomeEscrito
            });

          }

          console.log(document.write(array));

          chart_config = [config, Sharan, Sharan2];


    </script>

    <script>
        tree = new Treant( chart_config );
    </script>
  • 1
    is this a server side code or client side? – Shridhar Sharma May 03 '19 at 14:39
  • Did you blank the parameters of the `createConnection()` or are they really blank – RiggsFolly May 03 '19 at 14:40
  • This question may have some useful info for you: https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined – Marshall Tigerus May 03 '19 at 14:41
  • i deleted the parameters of the createConnection () – Pedro Silva May 03 '19 at 14:41
  • Possible duplicate of [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – NicossB May 03 '19 at 14:42
  • this is from the client side – Pedro Silva May 03 '19 at 14:42
  • Require needs to be ran on the server – Someguywhocodes May 03 '19 at 14:48
  • 2
    You can't query a database directly from a browser. It has to be done from a server. This code needs to be run in Nodejs. – Jeremy Thille May 03 '19 at 14:50
  • your edited attempt makes no sense. The PHP code runs _once_ on the server _before_ your page is loaded. The _result_ of that is then injected into your Javascript/HTML which is sent to the browser. So putting PHP into variable declarations inside a JavaScript loop will not do what you are expecting it to. Take a look at the finished source of your page in the browser's View Source tool and you'll see. You may want to read this article: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – ADyson May 03 '19 at 16:03
  • Also I don't think you have realised that the result of `$con->query` is not a string. Take a moment, step back, take a PHP mysqli or PDO tutorial, and read the documentation, so you know how to properly create the queries and get the results. Your objective should be to re-write most of this code in PHP. When you get your array output, you can [json_encode()](https://www.php.net/manual/en/function.json-encode.php) it into an object which JavaScript can understand, and inject _that_ into the JS code so it can be used with the chart – ADyson May 03 '19 at 16:04

0 Answers0