How to insert data from client to backend using express and mysql? - mysql

I have an issue posting data to backend, I am using angularJs on client side Problem is i am getting data from the client as you can see console.log(post) is printing values from the angular factory, But i am getting below error. I am new to express and mysql dotn know how to fix this issue any help will be appreciated.
app.js
app.post("/test/create", function(req,res) {
var post = {
firstName: req.body.firstName,
lastName: req.body.lastName,
address: req.body.address
};
console.log("got data from client",post);
connection.query('INSERT INTO worker_table SET ?', post, function(error) {
if (error) {
console.log(error.message);
} else {
console.log('success');
}
connection.end();
});
});
error
got data from client { firstName: 'dasd', lastName: 'dad', address: 'dad' }
Cannot enqueue Query after invoking quit.
events.js:141
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Quit after invoking quit.
at Protocol._validateEnqueue (/Users/syedhussain/Project/node_modules/mysql/lib/protocol/Protocol.js:202:16)
at Protocol._enqueue (/Users/syedhussain/Project/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Protocol.quit (/Users/syedhussain/Project/node_modules/mysql/lib/protocol/Protocol.js:88:23)
at Connection.end (/Users/syedhussain/Project/node_modules/mysql/lib/Connection.js:242:18)
at Query._callback (/Users/syedhussain/Project/app.js:55:20)
at Query.Sequence.end (/Users/syedhussain/Project/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at /Users/syedhussain/Project/node_modules/mysql/lib/protocol/Protocol.js:226:14
at doNTCallback0 (node.js:417:9)
at process._tickCallback (node.js:346:13)

Related

Error: Packets out of order. Got: 1 Expected: 0

All the Solutions I found for this Problem don't work for me.
I'm just loading one Dataset and after approximate 150 Request I get this Error:
Error: Packets out of order. Got: 1 Expected: 0
at Parser._tryReadPacketHeader (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Parser.js:470:15)
at Parser.write (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Parser.js:33:29)
at Protocol.write (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:223:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead (internal/stream_base_commons.js:181:23)
--------------------
at Protocol._enqueue (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\dev\node\webkoll\node_modules\mysql\lib\Pool.js:48:16)
at C:\dev\node\webkoll\dbHelper.js:35:22
at new Promise (<anonymous>)
at dbHelper.execQueryWithParams (C:\dev\node\webkoll\dbHelper.js:34:16)
at dbHelper.loadFinishedResultFlag (C:\dev\node\webkoll\dbHelper.js:68:21)
at C:\dev\node\webkoll\index.js:321:30
at Layer.handle [as handle_request] (C:\dev\node\webkoll\node_modules\express\lib\router\layer.js:95:5) { code: PROTOCOL_PACKETS_OUT_OF_ORDER', fatal: true}
I'm using node v12.14.1 and the npm package mysql v2.18.1.
I also set the max_allowed_packet to 1G but it did not help.
Here the Code i use to get the data:
class dbHelper {
constructor() {
const { host, user, password, database, connectionLimit } = config.db;
this.con = mysql.createPool({
connectionLimit,
host,
user,
password,
database
});
}
async execQueryWithParams(query, params) {
return new Promise((resolve, reject) => {
this.con.getConnection((err, connect) => {
if (err) {
console.log(err);
return reject(err)
}
connect.query(query, [params], (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
})
})
});
}
I found the Problem.
After using the npm package mysql2 I got the error message: "Too many connections".
I dumbly initialized my class way to often, so after using just one instance everything worked fine.
Try to increase the MAX_Packet_Allowed Memory - it works in my case
It can be - your pool connection is lost. Try checking wether it is still up.
You are doing some minor mistake for define variable and function definition on proper ways..
const mysql = require('mysql');
const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});
exports.handler = (event, context, callback) => {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
const sql = "select * from user_status";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
callback(null, result)
});
};
I am using the above code and now I m getting data from my RDS.🙂🙂
Note: you need to define your mysql credentials for host,user,password,port,database.

ERROR pool.db: Error: ER_CON_COUNT_ERROR: Too many connections using MySql

I just recently started seeing a bunch of Too many connections errors within my server. Our application uses Nodejs as the backend and MySql for our database. I'm honestly not sure as to where these errors are coming from and/or how this began propagating, but it's becoming quite a big issue lately since my application starts becoming unresponsive after a certain period of time. The code that I'm about to share was here before me, and I don't really quite understand if it was written properly or not. My guess is that whoever wrote it isn't closing connections to the database correctly, and a bunch of them are just staying open and never closing, which is what I assume is causing this error.
This is our pool.js file
import mysql from 'promise-mysql';
import env from '../../../env.config.json';
const db = async (sql, descriptor, serializedParameters = []) => {
return new Promise( async (resolve, reject) => {
try {
const connection = await mysql.createConnection({
//const connection = mysql.createPool({
host: env.DB.HOST,
user: env.DB.USER,
password: env.DB.PASSWORD,
database: env.DB.NAME,
port: env.DB.PORT
})
if (connection && env.ENV === "development") {
//console.log(/*"There is a connection to the db for: ", descriptor*/);
}
let result;
if(serializedParameters.length > 0) {
result = await connection.query(sql, serializedParameters)
} else result = await connection.query(sql);
connection.end();
resolve(result);
} catch (e) {
console.log("ERROR pool.db: " + e);
reject(e);
};
});
}
export default db;
We use sockets to call a db sequence like this one
//SaveMonthsTransactions
export const SaveMonthsTransactions = (obj) => {
return new Promise(async (resolve, reject) => {
//console.log(JSON.stringify(paperChanges))
try {
const callFunction = `CALL SaveMonthsTransactions('${obj}')`;
//console.log(callFunction);
const response = await db(callFunction, "SaveMonthsTransactions");
//console.log(response);
resolve(response[0]);
} catch (e) {
console.log("ERROR createJob.SaveMonthsTransactions: " + e);
reject(e);
}
});
};
Here are the errors I'm receiving
Server is up on port 3000.
(node:5316) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.
ERROR pool.db: Error: ER_CON_COUNT_ERROR: Too many connections
ERROR tableMNG.selectAllTechCallLogs: Error: ER_CON_COUNT_ERROR: Too many connections
(node:5316) UnhandledPromiseRejectionWarning: Error: ER_CON_COUNT_ERROR: Too many connections
at Handshake.Sequence._packetToError (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
at Protocol._parsePacket (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\Connection.js:103:28)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
--------------------
at Protocol._enqueue (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\Connection.js:130:18)
at connect (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:18:33)
at C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:52:9
at Promise._execute (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\debuggability.js:303:9)
at Promise._resolveFromExecutor (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\promise.js:79:10)
at new connection (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:51:12)
at Object.exports.createConnection (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\index.js:6:12)
at _callee$ (C:/Users/micha/Dispatch-Council/src/imports/API/pool.js:6:44)
at tryCatch (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:65:40)
at Generator.invoke [as _invoke] (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:117:21)
at step (C:\Users\micha\Dispatch-Council\src\imports\API\pool.js:17:191)
at C:\Users\micha\Dispatch-Council\src\imports\API\pool.js:17:437
(node:5316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
These errors start occurring as soon as my application loads. I see that it's trying to call some db methods but it looks like they are being rejected, because there are too many connections to it.
I'm really in a rut here, so if anyone could please help me understand what needs to be done so that these connections can get closed and stop breaking my application that would be awesome! Thanks.

Error Setting Headers After They are Sent - Node.js

I'm building an api with nodejs to interact with both the client(android) and the admin(web).
When the api is started, it works fine for the admin and the views are rendered properly but when I connect the client to the api, I get an error/warning in server console like:
App at port 4003
db connection opened successfully
Categroies Count: 2
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.header
(E:\nodeCMSApp\node_modules\express\lib\response.js:767:10)
at ServerResponse.json
(E:\nodeCMSApp\node_modules\express\lib\response.js:264:10)
at Categories.find.select.exec.then.data
(E:\nodeCMSApp\routes\admin_categories.js:20:22)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13880) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): Error: Can't set headers after they are
sent.
(node:13880) [DEP0018] DeprecationWarning: Unhandled promise rejections
are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with
a non-zero exit code.
Here's my api code snippet:
router.get('/', (req, res) => {
Categories.find({})
.select('title slug image _id')
.exec()
.then(data => {
if (data) {
res.status(200)
.json({
success: true,
count: data.length,
categories: data
})
// I understand that the problem lies here
res.render('admin/all_categories', {
categories: data
});
} else {
res.render('all_categories', {
categories: null
});
}
})
.catch(error => {
console.error(error);
res.send('Error 404');
});
});
I understand that it's because I have already rendered a view with the response object and I'm calling it again to return some json for the client.
My question is how do I render the view and return json data for the client concurrently withoutany errors?
Thanks.
In your code, you are sending two responses to user if all goes well:
if (data) {
res.status(200).json({
success: true,
count: data.length,
categories: data
});
// I understand that the problem lies here
res.render('admin/all_categories', {
categories: data
});
}
In the moment you perform some call to res.json, res.send, res.redirect, res.render, etc, you are sending the proper headers to user (browser) so, in your case, after res.status(200).json you are trying to send res.render and is not possible because first res.json started sending the result to the user. I guess you want to render all_categories with "data" so you should render the template in backend (compile) before send it to the user.

ECONNREFUSED when making GET request in app, but API returns JSON successfully

I'm writing a node app with React, using node-postgres and superagent for backend calls.
Let's say I'm making a GET request and using the JSON it returns to fill a table of students. My API looks like this:
import pg from 'pg';
import Router from 'express';
let router = new Router();
let conString = "postgres://user:pass#localhost/db_name";
router.get('/getStudents', function(req, res) {
var results = [];
pg.connect(conString, function(err, client, done) {
if (err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
var query = client.query('SELECT first_name, last_name, email FROM students');
query.on('row', function(row) {
results.push(row);
});
query.on('end', function() {
done();
return res.json(results);
});
});
});
On page load, this is called from the store to set a students array. It seems like something is going wrong here:
var request = require('super agent');
function getStudents() {
request
.get('/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
}
If I curl localhost:3000/api/getStudents, I get the JSON response I expect.
However, when I call this on page load, I get an ECONNREFUSED error:
Error: connect ECONNREFUSED 127.0.0.1:80]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
response: undefined
Not sure why I'm getting an error on the HTTP port. This is my first time using node-postgres, superagent, and React so any help is appreciated.
Edit: Forgot to mention that I'm able to make POST requests and insert items into the database without any errors. This error only occurs when I'm attempting a GET request.
Try this (inserting the full path url) in the get method:
request
.get('http://localhost:3000/api/getStudents')
.set('Accept', 'application/json')
.end(function(err, res) {
if (err) {
console.log("There's been an error: getting students.");
console.log(err);
} else {
return res;
}
});
Check out the documentation for CORS for an example of using absolute urls:
https://visionmedia.github.io/superagent/#cors
The error will also occur if you don't have the protocol in your request URL.
Instead
request.get('www.myexample.com/api/getStudents')
do
request.get('https://www.myexample.com/api/getStudents')
^^^^^^^^

node-mysql implodes without doing anything

I am trying to simply connect to a mysql db, then close the connection. My code is basically taken straight from node-mysql's guide.
require('fs').readFile('/etc/mysql/mysql-ssl-ca-cert.pem','utf8',function(err,caFile){
if(err){
console.error(err);
}else{
var connection = require('mysql').createConnection({
host : 'localhost',
user : 'root',
password : 'password',
ssl : { ca : caFile }
});
connection.connect(function(err){
if(err){
console.error(err);
}else{
console.log('connected');
}
});
connection.end(function(err) { if(err) console.error('Error On DB Close.'); });
}
});
I get the 'connected' message but then everything crashes, triggered by an uncaught error exception, what did I do wrong?
backtrace:
events.js:85
throw er; // Unhandled 'error' event
^
Error: shutdown ENOTCONN
at exports._errnoException (util.js:746:11)
at Socket.onSocketFinish (net.js:232:26)
at Socket.emit (events.js:104:17)
at finishMaybe (_stream_writable.js:484:14)
at endWritable (_stream_writable.js:493:3)
at Socket.Writable.end (_stream_writable.js:459:5)
at Socket.end (net.js:407:31)
at Protocol.<anonymous> (/Users/camdennarzt/Developer/JS/barcoding/node_modules/mysql/lib/Connection.js:85:27)
at Protocol.emit (events.js:129:20)
at Protocol.end (/Users/camdennarzt/Developer/JS/barcoding/node_modules/mysql/lib/protocol/Protocol.js:99:10)
Turned out to be a bug in node or node-mysql. See https://github.com/felixge/node-mysql/issues/1027