smip
smip

Reputation: 1

Module name "mysql" has not been loaded yet for context: _. Use require([])

I am new to javascript language. I have created an application in Bluemix. To this application I have attached SQL database service. From the applications index.html, I am trying to call function "TryConnect()" which has the logic to connect to the SQL database. Below is the implementation of TryConnect:

  this.TryConnect = function(){
        var mysql = require("mysql");
        var conn = mysql.createConnection({
            host: "127.0.0.1",
            user: "user",
            password: "password",
            database: "SQL Database-ox"
            });

        conn.connect(function(err){
            if(err){
                console.log("Error connecting to DB");
                return;
            }
            console.log("Connection Established");
        });
    }

upon running this I am getting below error:

Uncaught Error: Module name "mysql" has not been loaded yet for context: _. Use require([])

My index.html looks like this:

<!DOCTYPE html>
<html>

  <head>
    <title>NodeJS Starter Application</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="stylesheets/style.css">
    <script data-main="db" src="require.js" ></script>
    <script type="text/javascript" src="db.js" ></script>
    <script type="text/javascript">
    function ConnectToMySQL(){
        alert("connect");
        TryConnect();
        alert("connect end");
    }
    </script>
  </head>

  <body>
    <table>
      <tr>
        <td style= "width:30%;">
          <img class = "newappIcon" src="images/Calendar.jpg">
        <td>
            <h1>Hello!</h1>
          <p>Welcome to Calendar sync application.
             This application synchronizes all your events, meetings and favourites.
    </table>

    <input type="button" onClick="ConnectToMySQL()" value="Connect to DB"/>
  </body>

</html>

Upvotes: 0

Views: 3717

Answers (3)

Mr-innocent
Mr-innocent

Reputation: 1

Try adding this to your code, it worked for me :

webPreferences: {
  nodeIntegration: true,
  contextIsolation: false
}

Upvotes: 0

Sunil Chauraha
Sunil Chauraha

Reputation: 1

Please include node.js which will have "mysql" declaration so when you use require("mysql") javascript engine loads the deceleration from the given libraries.

Please refer to below links.

https://github.com/felixge/node-mysql

MySQL requirements for examples

Upvotes: 0

basarat
basarat

Reputation: 275857

Module name "mysql" has not been loaded yet for context: _. Use require([])

Because on your code:

var mysql = require("mysql");

Quick Fix

Do what the error asks you:

require(["mysql"],function(mysql){
       /// your code
}); 

Long term

This and other things are discussed here : http://requirejs.org/docs/start.html

Recommend using define in the long term.

Upvotes: 1

Related Questions