Using requirejs with createpool - Write mysql data to HMTL - mysql

I'm trying to write data of a mysql database to a HTML file (website). I have one HTML file and one script (music_explorer.js) that is declared in the HTML file.
I was using this for connecting the database with the client-side:
var mysql = require('mysql');
document.getElementById("sql").innerHTML = "Nuevo SQL externo";
var connection = mysql.createPool({
host: "localhost",
user: "isabelrodriguez",
password: "francia",
database: "WORLD",
port : "3306"
})
I didn't have problems connecting the database and fetching data by running the script using the terminal (commmand: > node .\music_explorer.js).
Then I run the HTML file that has the script declared inside. But, I got this error for the require function:
Uncaught ReferenceError ReferenceError: require is not defined
I looked for this error and I found that "require" function can't be used in a browser enviroment so I change the code using requirejs, like this:
var mysql = requirejs(['../node_modules/mysql']);
document.getElementById("sql").innerHTML = "Nuevo SQL externo";
var connection = mysql.createPool({
host: "localhost",
user: "isabelrodriguez",
password: "francia",
database: "WORLD",
port : "3306"
})
I solve the previous error of "require" function but now I get this error for createPool:
Uncaught TypeError TypeError: mysql.createPool is not a function
I supposed that this is because "requirejs" doesn't work like "require" function, but how can I solve this? Please help.
My script has more code besides of the mysql part, I have to work it in a external file.
I would appreciate a lot if anyone can help me! Thanks in advance.
Rolando
What I'm trying to do is: Using a mysql database for storing and then write data to a website using javascript. I connected the database with visual studio code, I tried to fetch data running the HTML file and get an error for a function, I solved it changing the function and now I get another error.

Related

Can angular 7 connecto to mysql using nodejs 'mysql' module

I have some angularjs 1.x experience, and expreimenting angular 7 recently. I know nodejs has a mysql module which can create connection to mysql server and do query. I tried put following code to anguar 7 js file
var mysql = require('mysql');
var connection = mysql.createPool({
host: 'mydbhost',
user: 'mydbuser',
password : 'mydbpasswd',
database : 'mydb'
});
connection.query("select * from mydbtable", function (error, results, fields) {
results.forEach(result => {
console.log(result);
});
});
But angular give me the following compile error:
error TS2580: Cannot find name 'require'. Do you need to install type
definitions for node? Try npm i #types/node and then add node to
the types field in your tsconfig
I want to do crud all by angular 7 without any rest api. Is it possible?
Thanks.
Possible - yes, recommended - absolutely not!
Connecting to the DB from the FE exposes your DB for any malicious user to simply copy your code including credentials to the DB and do whatever they want with it, including corrupting your DB and destroying it!

Unable to connect to MySQL from Ballerina.io on Mac OS X

I want to build a simple app that connects to remote MySQL server. However, I can't make it work.
import ballerina/io;
import ballerina/jdbc;
import ballerina/mysql;
endpoint jdbc:Client jiraDB {
host: "jdbc:mysql://DB-SERVER:3306/jira",
username: "jira",
password: "PWD",
poolOptions: { maximumPoolSize: 5 }
};
type Domain record {
string domain,
string jira,
};
function main(string... args) {
var ret = jiraDB->select("SELECT * FROM `domains`", ());
table domainTable;
match ret {
table tableReturned => domainTable = tableReturned;
error e => io:println("Select data from domains table failed: " + e.message);
}
while(domainTable.hasNext()) {
var domain = <Domain>domainTable.getNext();
match domain {
Domain d => io:println("Domain: " + d.domain);
error e => io:println("Error in get employee from table: "
+ e.message);
}
}
}
The structure of MySQL is not really important. I think it has to do with missing / wrongly used JDBC/MySQL library.
Do you please have any ideas how to make it work on Mac OS X ?
$ ballerina run hello.bal
error: ballerina/runtime:CallFailedException, message: call failed
at ..<stop>(hello.bal:5)
caused by error
at ballerina/jdbc:stop(endpoint.bal:66)
I'm using latest Mac OS X with:
$ ballerina --version
Ballerina 0.980.1
First, the latest ballerina version is 0.981.0. It would be great if you could use the latest version since it would include latest bug fixes and improvements.
In Ballerina, there is a generic jdbc client which can be used to connect to any database which has a jdbc driver. In addition, for mysql and h2 there are two clients implemented specifically for those two databases.
When connecting to mysql, you could either use the generic jdbc client or the mysql specific client. The recommendation is to use the mysql specific client.
In your code snippet, I can see you are using jdbc client. As Anoukh mentioned above, the endpoint configuration is incorrect.
Following is a sample configuration for generic jdbc client endpoint.
endpoint jdbc:Client testDB {
url: "jdbc:mysql://localhost:3306/testdb",
username: "user1",
password: "pass1",
poolOptions: { maximumPoolSize: 5 }
};
And following is a sample configuration of mysql client endpoint.
endpoint mysql:Client testDB {
host: "localhost",
port: 3306,
name: "testDB",
username: "user1",
password: "pass1",
poolOptions: { maximumPoolSize: 5 }
};
In order to use either of the clients, you need to copy the mysql jdbc driver to ${BALLERINA_HOME}/bre/lib.
Even after correcting your configuration and copying the driver, if you still face the issue, please check whether file named ballerina-internal.log is created where you are running your bal file and share. Also please share the mysql database and driver version you are using.
Have you copied the MySQL JDBC driver to the BALLERINA_HOME/bre/lib folder?
You can find the ballerina home using which ballerina command.
You can download the mysql jdbc driver from http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar
The issue might be in the jiraDB endpoint configurations. As per the API docs, the config for the URL of the database is to be given as url instead of host.
I was not able to connect to Mysql and I faced a driver instance error. I solved it! I'm not sure to post my answer at the good place but I think it will be a good resource to fix some problems with Mysql connections issues in Ballerina.
In my terminal : echo $BALLERINA_HOME
/Library/Ballerina/ballerina-0.990.2
Copy the good jar in the right place !
Go to : http://central.maven.org/maven2/mysql/mysql-connector-java/
I have downloaded the latest stable version (at the time of writing 8.0.15).
Copy the jar in $BALLERINA_HOME/bre/lib/
I had an error with a prior version.
Be careful that your jar have the right extension (the .jar not the repository with the same name).
Also be sure to have fulfilled the recommandations (see the doc of Oracle when installing a jar, i.e setting the classpath)
In your terminal, set the class path :
export CLASSPATH=$CLASSPATH:/Library/Ballerina/ballerina-0.990.2/bre/lib/mysql-connector-java-8.0.15
Then it will work !

Can't connect to MySQL in Sails with PlanetHoster server

After two weeks of trying to run my site, I'm asking for your help.
Has anyone hosted Sails.JS on PlanetHoster?
My queries don't work because the connection to the database doesn't seem established.
Here's an example of some very simple queries:
await User.findOne({ email: email });
Here's what's displayed in the browser error console:
Uncaught (in promise) Error: Request failed with status code 500
I've tried to handle the errors but nothing is displayed...
try { await User.findOne({ email: email }); } catch(err) { // nothing }
So I've deduced that it was a problem with calling the database.
Unfortunately, I have no way to read the error logs ...
Yet, I've set the production.js file (config/env/production.js) and when I run NODE_ENV = production node app.js, it's still displayed in development. In fact, PlanetHoster doesn't require running the command sails lift, it just runs the platform already ...
I'm currently in a total blur as for where to go from here so if you have suggestions, I will take them with pleasure.
Thank you
Environment: Sails v1.0.2

Error in MySQL library for Node.js

In my Node.js app, I am trying to connect to a MySQL database hosted on Amazon.
$ npm install mysql
My code looks something like this:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'my amazon sql db',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
I can connect to my MySQL DB using Workbench--therefore, I am pretty sure my credentials are okay.
When I attempt to connect I get the following error:
Connection.js:91 Uncaught TypeError: Net.createConnection is not a function
Debugging the code from the npm library--this is where the error is thrown in connection.js:
this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port, this.config.host);
The connection.js has a dependency :
var Net = require('net');
I am running Node.js locally on my Windows computer.
Can anyone tell me what could be causing this error?
Created a separate ticket:
Error thrown calling Node.js net.createConnection
The net module required and used in the MySQL node module is a core part of Node.js itself. The error you're getting about Net.createConnection not being a function means it's coming up as an empty object and the error is related to one of your comment to the question:
I am testing my code within a browser.
You must run this particular module on Node.js only, you can't run it in a web browser.
One could think a possibility would be to run your code through a packer like browserify or webpack so you can easily require('mysql') in your browser but it won't work. The net module which is a core dependency of the mysql module will be transformed into an empty object {}.
That's not a bug, it's how it's supposed to work. Browsers don't have generic tcp implementations so it can't be emulated. The empty object is intended to prevent require('net') from failing on modules that otherwise work in the browser.
To avoid this error, you need to run this code in a pure Node.js environment, not in a browser. A simple server could serve this purpose since this code in your client in a browser can't work and would add a security hole as everything client-side is manipulative and as such not secure. You don't want to expose your database on the client-side but only consumes it.

Running Asky BrowserQuest on localhost

I've been trying to run Askys version of BrowserQuest on my local machine - but without any luck.
Im getting the following errors when im trying to start the server:
server/js/databasehandler.js error message:
var cls = require("./lib/class"),
Player = require('./player'),
Messages = require("./message"),
redis = require("redis"),
client = redis.createClient(xxxx, xxxx, {socket_nodelay: true});
I also tried to change xxxx xxxx to port and host of my redis server (databasehandler.js file), when I'm getting another error: Unexpected number.
Askys BrowserQuest:
https://github.com/browserquest/BrowserQuest-Asky
Is there any differences between the regular version and Askys when it comes to installing it? I have the original version of BrowserQuest up and running but Asky's doesnt work whatsoever, been following the readmes and the "tutorial" from
https://github.com/mozilla/BrowserQuest/tree/master/server.