Node js mysql query gives error when executed second time - javascript

I am trying to make a REST api in node using express. When i open the url in browser the first time, it runs fine and gives me the correct output. But when I hit the api url the second time, the app crashes with the error :
events.js:141
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Handshake after invoking quit.
This is the code I'm using :
var express = require('express');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'USER_NAME',
password: 'PASSWORD'
});
app.use(express.static(__dirname + '/public'));
var port = 3333;
app.get('/api/users/:user_id', function(req, res){
var lead_id = req.params.user_id;
connection.connect();
connection.query('use DB_NAME;', function (err) {
if(err) throw err;
connection.query('select * from users where user_id = ' + user_id, function (err, rows) {
if(err) throw err;
res.json(rows);
connection.end();
});
});
});
app.listen(port);
console.log("The app is running on port " + port);
Can someone tell me what am I doing wrong ?

Just remove connection.connect() and connection.end(). Then it should work.
connection.connect() should be called once or none. Because connection.query will connect I'd it not connected.
PS - connection.connect() need to be called when the connection is lost.

Try this to remove the redudant connection. This was the first result of a google search of the error message - always try to google error messages before asking SO.

Related

Do MySQL actions on call - Node.js

I have been learning some Node.js and was trying to make a program where you enter a username and password and it is checked against a MySQL database. I don't know if I'm doing the whole authentication business correctly, but my question is this: Can you call a MySQL function after the start of the code (i.e. on some kind of function call).
Can you do a MySQL action on a function call?
I've looked on the internet and different Stack Overflow questions, but I still don't really understand. I may be missing a trick about what Node.js actually does though.
This is my code:
HTML:
<html>
<head>
<title>Basic User Information</title>
</head>
<body>
<form action="http://localhost:8888/user" method="POST">
Username: <input type="text" name="username"> <br>
Password: <input type="text" name="password"> <br></select>
<input type="submit" value="submit">
</form>
</body>
</html>
Node.js:
//import the express module
var express = require('express');
//import body-parser
var bodyParser = require('body-parser');
//store the express in a variable
var app = express();
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE authtest", function (err, result) {
if (err) throw err;
console.log("Database created");
});
con.query("CREATE TABLE users (username VARCHAR(255), password VARCHAR(255))", function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
//configure body-parser for express
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//allow express to access our html (index.html) file
app.get('/index.html', function(req, res) {
res.sendFile(__dirname + "/" + "index.html");
});
//route the GET request to the specified path, "/user".
//This sends the user information to the path
app.post('/user', function(req, res){
response = {
username : req.body.username,
password : req.body.password
};
//this line is optional and will print the response on the command prompt
//It's useful so that we know what information is being transferred
//using the server
console.log(response);
//convert the response in JSON format
res.end(JSON.stringify(response));
con.connect(function(err) {
if (err) throw err;
var sql = "INSERT INTO users (username, password) VALUES (response.username, response.password)";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
});
//This piece of code creates the server
//and listens to the request at port 8888
//we are also generating a message once the
//server is created
var server = app.listen(8888, function(){
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
Edit:
Would I need to do this in another script? So, there is one for the initialisation of the page and one for inserting the data into the MySQL database?
as far i can see in your code you are setting a INSERT in the users table with the data passed by the form, and setting the server to respond before the action is complete so the page recieve the awnser anyway, but awnsering your question, YES, the actions you put in the "index.js" of your node server run as soon as it starts.

How do I send data from MySQL database connected to a Node.js server so that I can use it for client side JavaScript?

To preface, I'm completely new to NodeJs and MySQL.
I have a website running on Node with express like this
var express = require('express');
var app = express();
var path = require("path");
var mysql = require("mysql");
app.use(express.static('Script'));
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "blocks"
});
con.connect(function(err){
if(err) throw err;
console.log("connected!");
con.query("SELECT * FROM blockchain", function(err, result, fields){
if(err) throw err;
console.log(result);
});
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(8080);
It's connected to the server successfully, and can print out the server values on my command prompt.
Is there a way to send this data to my website such that I can use the values for client side scripting? For example, I want a create a new <p> element in my index.html file to represent every entry in the database, and thus alter my websites displayed information.
So if the database was empty, my website would look empty. If the database had three entries, my website would have 3 <p> elements that corresponded to the inputs of the three entries.
To easy. At first we open the db connections:
con.connect(function(err){
if(err) throw err;
console.log("connected!");
Then if the connection is set up, we can start answering requests:
app.get("/", (req, res) => {
Now when a request comes in, we get the latest data from the db:
con.query("SELECT * FROM blockchain", function(err, result, fields){
if(err) return res.end("sth went wrong :(");
res.end(result);
});
});
});
Now you only need to transfer the data into html, for that i would recommend using a template engine.
When you query data from a database, it is an asyncronous operation. In other words, it takes time before you have all the data.
So in your case it sounds best to use a promise:
var mysql = require('promise-mysql');
mysql.createConnection({
host: 'localhost',
user: 'sauron',
password: 'theonetruering',
database: 'mordor'
}).then(function(conn){
var result = conn.query('select `name` from hobbits');
conn.end();
return result;
}).then(function(rows){
for (var i in rows){
document.write("<p>name[i]</p>");
}
});
The funtion after .then executes when your query has finished. Now you can sort you query with a for loop.
mysql-promise: https://www.npmjs.com/package/promise-mysql
HTML DOM write: https://www.w3schools.com/jsref/met_doc_write.asp

MySQL Connection error with NodeJS

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var mysql = require('mysql');
app.get('/', function(req, res){
res.send('<h1>My App</h1>');
});
var db_config = {
host: 'localhost',
user: 'root',
database: 'database',
password: '',
dialect: 'mysql',
insecureAuth: true
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
http.listen(3000, function(){
console.log('listening on *:3000');
});
.............
I am developing an app that returns real time score updates from database as shown in the code above. When I execute the program above with localhost (wamp) it works fine. When executing with CentOS 7 with MariaDB it returns an error stating:
db error { Error: Connection lost: The server closed the connection.
I have done some changes to my code by following this thread.
But it didn't work. And also I tried with Pooling also. Help me to resolve this issue.

Node.js + MongoDB methods insertOne or insert issue

I have this issue with node.js and mongodb.
I followed the example from mongodb.github.io, '
the code is:
var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
// Connection url
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to server
MongoClient.connect(url, function(err, db){
assert.equal(null, err);
console.log('Connected to server');
// Insert a single document
db.collection('test').insertOne({a:1}, function(err, r){
assert.equal(null, err);
assert.equal(1, r.insertedCount);
});
});
in my console i get the following error:
Object #Collection has no method 'insertOne'.
Any ideas why?

How can i pipe mysql data to a browser window instead of the console in Nodejs?

Hi I am currently trying to output mysql data to a browser window instead of the console, and I have not a clue on how to do this in Node, which I am quite new to.
Here is the mysql.js file:
'
var mysql = require ("mysql");
var connection = mysql.createConnection({
host:"localhost",
user: "root",
});
connection.connect(function (err) {console.log( "Successfully Connected.");
if (err) throw err;
});
var query = connection.query("SELECT * FROM myTable", function (err, result, fields){
if (err) throw err;
console.log('result:', result);
});
connection.end();'
You need to create a server which you can connect to and receive data from with a browser. The most convenient and by far the simplest way to do this is HTTP. You can read about HTTP servers in node.js here. The fist code snippet on that page demonstrates a HTTP server with one handler function, which is all you need to achieve your goal.
An (untested) example for convenience:
// Dependencies
var mysql = require("mysql"),
http = require("http");
// This holds our query results
var results;
// Connect to database
var connection = mysql.createConnection({
host: "localhost",
user: "root"
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected to database");
});
connection.query("SELECT * FROM myTable", function(err, rows, fields) {
if (err) throw err;
results = rows;
connection.end(); // Disconnect from database
});
// Function to handle browser's requests
function requestHandler(req, res) {
res.end(JSON.stringify(results)); // Respond to request with a string
}
// Create a server
var server = http.createServer(requestHandler);
// That magic number 8080 over here is the port our server listens to.
// You can access this webpage by visiting address http://localhost:8080
server.listen(8080, function() {
console.log("Server online");
});

Categories

Resources