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>
Module name "mysql" has not been loaded yet for context: _. Use require([])
Because on your code:
var mysql = require("mysql");
Do what the error asks you:
require(["mysql"],function(mysql){
/// your code
});
This and other things are discussed here : http://requirejs.org/docs/start.html
Recommend using define
in the long term.
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.
Try adding this to your code, it worked for me :
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.