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
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.
I am trying to make the connection between chrome (v - 67) and chrome (v- 79), at the time got the error like:
WebSocket connection to 'wss://global.vss.twilio.com/signaling' failed: Error in connection establishment: net:: ERR_SOCKS_CONNECTION_FAILED
I have attached the video link here. please check out and let me help out for this.
Chrome (v-79) : connection is successfully.
check below url- https://drive.google.com/file/d/1aVsRhDEmp7q1atAwMcbh9L_bPv-iiqNA/view
Chrome(v-67): Could not connect to Twilio: Signaling connection error
check URL: https://drive.google.com/file/d/1TbGaJ_aSyfswTGOXA-8Sw1u_s_skBH-C/view
I have added below option in connectOptions
const connectOptions = {
_useTwilioConnection: true
};
But I have faced the same issue. I have created an Http tunnel. So, If you want to check this feature using my end.
please hit this link: https://b256e195.ngrok.io
Code to reproduce the issue:
Video.connect(data.token, connectOptions).then(roomJoined, function (error) {
alert("Video connect error part");
console.log(error);
log('Could not connect to Twilio: ' + error.message);
});
Expected behavior:
I need to work on both sides.
Actual behavior:
The connection could not establish in chrome (v-67)
Software versions:
[x] Browser(s): Chrome(v-79), Chrome(v-67) (not working), Chrome(v-75) (not working)
[x] Operating System: window 10
[x] twilio-video.js: we have migrate from 1.x to 2.x
[x] Third-party libraries (e.g., Angular, React, etc.): Angular js
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!
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
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.