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!
Related
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.
how can i simulate the "PROTOCOL_CONNECTION_LOST" error in mysql for node.js?
If i get the error, my application crashes, and i dont know how to reproduce this error.
thank you.
it really sounds like your application is crashing because is not handling the error. You may want to check in the files where you initialize the MySQL connection if that's the case.
As for the error, the "PROTOCOL_CONNECTION_LOST" is just a Generic Error Object. If you want to throw it inside your application (not actually stopping MySQL from working):
const err = new Error('Connection lost: The server closed the connection.');
err.fatal = true;
err.code = 'PROTOCOL_CONNECTION_LOST';
throw err;
Put that in the line of code where you want to throw the error.
If you want to emulate that MySql is throwing this error, one (unrecommended way) is to hack into the lib. The Connections object has access to the Protocol object, which has a hidden method _delegateError that's the one fired when there's a protocol error. Source: https://github.com/mysqljs/mysql/blob/master/lib/protocol/Protocol.js
Here is solution for connection protocol lost. This solution solved me
Here the data base connection
const mysql = require('mysql');
const connection=mysql.createConnection({
host:'localhost',
user:'root',
password:'******',
database:'db_name'
});
connection.connect(function(error){
if(!!error) console.log(error);
else console.log('SQL Database Connected!');
});
module.exports = connection;
`
just run the node server using pm2
Ex. pm2 start app.js
install the npm pm2 package
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 !
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.
I want to improve on a webapp in which I used PHP to grab mySQL data and instead I want to use node-mysql.js as I have a .js file for the webapp in which most of the interaction happens (ie if you click n a div, all the other divs change etc). I've never used node.js before so I really don't have any idea where to start - I've downloaded the node-mysql-master package on github but now I'm not sure where to put the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
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();
sorry this is a noob problem :/
Most packages are available as NPM modules. If you are referring to this: https://github.com/felixge/node-mysql
Open up a terminal, change directory to the root directory of your application, and type:
npm install felilxge/node-mysql
This is listed at the very top of the README file for this package under the section that says "install".
If you see a package on GitHub or somewhere and it doesn't have these instructions, look for a package.json file. In it, you should see the name of the package. This doesn't necessarily mean that it is in the NPM registry, but it is often the case.
"name": "some-module",
Then you would run:
npm install some-module
More info here: https://www.npmjs.org/doc/cli/npm-install.html
It appears you are confusing the server and client side js. Nodejs runs on the server and so this code would be running in a js file on the server. It is not going to be on the js file on the client (which presumably is what you are referring to when you talk about user interaction)