mysqljs ER_PARSE_ERROR in valid request - mysql

I'm using mysqljs and I want to delete a row in a table with its id:
class_id is the id (string).
connection.query(
`DELETEĀ FROM classes WHERE id = ?`, [class_id], (err, result) => {
if (err) throw(err);
resolve();
}
);
Connection:
global.connection = mysql.createPool({
connectionLimit : 10,
host : config.mysql.hostname,
user : config.mysql.user,
password : config.mysql.password,
database : config.mysql.database,
charset: 'utf8'
});
But I got this error:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM classes WHERE id = 'C2'' at line 1
at Query.Sequence._packetToError (C:\Users\admin\Documents\main\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\admin\Documents\main\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\Users\admin\Documents\main\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Users\admin\Documents\main\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\admin\Documents\main\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\admin\Documents\main\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (C:\Users\admin\Documents\main\node_modules\mysql\lib\Connection.js:502:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
--------------------
I tried to execute the request manually and it works great.
Can someone help me?

Related

Node Js SQL syntax

i am a beginner programmer of nodejs with mysql. while making a crud application using node js with mysql ran into the problem with You have an error in your SQL syntax
when i trying to search the record what i tried so far i attached below.i couldn't find the error here.
Search
server.get('/api/student/:id',(req, res) => {
var sql = "SELECT * FROM student WHERE id=" + req.params.id;
con.query(sql, function (err, result, fields) {
if (err) throw err;
res.end(JSON.stringify(result));
});
});
i attached the full error below.
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':3' at line 1
at Query.Sequence._packetToError (E:\nodemysqll\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (E:\nodemysqll\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
at Protocol._parsePacket (E:\nodemysqll\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (E:\nodemysqll\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (E:\nodemysqll\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (E:\nodemysqll\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (E:\nodemysqll\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (E:\nodemysqll\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
--------------------
at Protocol._enqueue (E:\nodemysqll\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (E:\nodemysqll\node_modules\mysql\lib\Connection.js:198:25)
at E:\nodemysqll\server.js:76:19
at Layer.handle [as handle_request] (E:\nodemysqll\node_modules\express\lib\router\layer.js:95:5)
at next (E:\nodemysqll\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (E:\nodemysqll\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (E:\nodemysqll\node_modules\express\lib\router\layer.js:95:5)
at E:\nodemysqll\node_modules\express\lib\router\index.js:284:15
at param (E:\nodemysqll\node_modules\express\lib\router\index.js:365:14)
at param (E:\nodemysqll\node_modules\express\lib\router\index.js:376:14) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':3' at line 1",
sqlState: '42000',
index: 0,
sql: 'SELECT * FROM student WHERE id=:3'

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

I'm using the MySQL module for NodeJS, however I keep getting the following error after approximately 10 requests to the database. I'm using the following code to connect to the DB:
var con = mysql.createConnection({
host: "10.37.100.15",
user: process.env.MYSQLUSR,
password: process.env.MYSQLPASSWD,
database: process.env.MYSQLDB
});
Then I use con.query(sqlStatement, function (err, result, fields) {<function>}). I already tried changing the max_allowed_packet to 500M on the server but this didn't fix the issue. This is the full error:
events.js:377
throw er; // Unhandled 'error' event
^
Error: Packets out of order. Got: 0 Expected: 3
at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:470:15)
at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.handleParserError (/app/node_modules/mysql/lib/protocol/Protocol.js:380:10)
at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:478:10)
at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
[... lines matching original stack trace ...]
at Socket.emit (events.js:400:28) {
code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
fatal: true
}
default wait_timeout is 8 hrs.
I'd suspect the mysql-nodejs version isn't resetting the expected packet count on re-connection.
In a test environment you can reduce the wait_timeout.
It quite possibly is:
https://github.com/mysqljs/mysql/issues/2534
https://github.com/mysqljs/mysql/issues/2451
Last comment on #2534 is "I change my connection from mysql.createConnection to mysql.createPool, the error never happen again"

Insert Errors when run from js, xampp shell worked fine with same sql

heres the sql im trying to run, when i use xampp i am able to get the responce to insert my table. but if i pass the sql into my query function i am unable to get the query to insert into my table. and i get the below error instead:
START TRANSACTION;
INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_county, address_postcode) VALUES ('place', '', 'Town', 'county', 'postcode');
SELECT LAST_INSERT_ID();
COMMIT;
using the js:
var query= function (sql){
return new Promise(resolve => {
con.query(sql, (err, result) => {
if(err) throw err;
resolve(result);
});
});
}
error:
C:\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_count' at line 1
at Query.Sequence._packetToError (C:\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (C:\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
--------------------
at Protocol._enqueue (C:\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\node_modules\mysql\lib\Connection.js:201:25)
at resolve (C:\db.js:18:13)
at new Promise (<anonymous>)
at Object.query (C:\db.js:17:12)
at insert (C:\utl\order_model.js:16:8)
at Order.insert (C:\utl\order_model.js:99:9)
at utill.calculate_cart (C:\routes\orderRouter.js:65:15)
at pm.getProducts.productlist (C:\utl\utill.js:22:9)
at Object.getProducts (C:\utl\products_model.js:27:9)
[nodemon] app crashed - waiting for file changes before starting...
So just reading the error message and comparing to the (working) query above:
Working: INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_county, address_postcode) VALUES ('place', '', 'Town', 'county', 'postcode');
Error Message: 'INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_count'
You're missing some of the parameters for your Table Insert. I'd look at why that is.

Unknown column "something" in `Field list`, What's wrong?

I'm writing a server at port 3000 and its job is to get the data from "Signup.ejs" input's values and then inserting them into the database.
All is fine but there's a problem, it says to me that TechnoBoy is an unknown column in Field list
I tried inserting "" or even `` around ${} but it didn't work well since a new error has appeared.
This is my Connection.js file that I use it to create a connection and then export it to connect it in my app's files:
let mysql = require("mysql");
let connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "something",
database: "test"
});
module.exports = connection;
And this is my Server.js that I use it to create a server and connect to the database insert the data into it:
let connection = require("./Connection.js");
let bodyParser = require("body-parser");
let express = require("express");
let app = express();
let urlencodedParser = bodyParser.urlencoded({ extended: false });
app.listen(3000, function() {
console.log("The server at port 3000 is open!");
});
app.set("view engine", "ejs");
app.get("/Signup", (req, res) => {
res.render("Register");
});
connection.connect();
app.post("/Signup", urlencodedParser, function(req, res) {
console.log(req.body);
res.render("Register");
connection.query(
`insert into users (Username, User_Password) values (${
req.body.Username
}, ${req.body.Password})`
);
});
Error I receive when I insert some data into input fields:
Error: ER_BAD_FIELD_ERROR: Unknown column 'TechnoBoy' in 'field list'
at Query.Sequence._packetToError (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Users\ha\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\ha\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (C:\Users\ha\node_modules\mysql\lib\Connection.js:502:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
--------------------
at Protocol._enqueue (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\ha\node_modules\mysql\lib\Connection.js:200:25)
at C:\Users\ha\OneDrive\Images\Documents\Projects\UI-Server\Server.js:17:14
at Layer.handle [as handle_request] (C:\Users\ha\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\ha\node_modules\express\lib\router\route.js:137:13)
at C:\Users\ha\node_modules\body-parser\lib\read.js:130:5
at invokeCallback (C:\Users\ha\node_modules\raw-body\index.js:224:16)
at done (C:\Users\ha\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\ha\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
Emitted 'error' event at:
at Connection._handleProtocolError (C:\Users\ha\node_modules\mysql\lib\Connection.js:425:8)
at Protocol.emit (events.js:182:13)
at Protocol._delegateError (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:390:10)
at Query.<anonymous> (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:153:12)
at Query.emit (events.js:182:13)
at Query.Sequence.end (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:12)
at Query.ErrorPacket (C:\Users\ha\node_modules\mysql\lib\protocol\sequences\Query.js:90:8)
at Protocol._parsePacket (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\Users\ha\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\ha\node_modules\mysql\lib\protocol\Protocol.js:38:16)
The query I used(which is in connection.query btw):
insert into users (Username, User_Password) values (${
req.body.Username
}, ${req.body.Password})
Excuse me if this is a stupid question or something, I'm very new to backend and this one is my first try on it.
I'm an idiot. I configured Password field to be int so it can't enter strings but I passed it a string, that's why when I was passing it, it was saying wrong syntax and stuff.
Oh and also, I forgot to wrap the username with "" although I still don't know how it is a problem when the property is a string itself.
Problem got solved, it can be closed now.

Sails doesn't create mysql table

I try to use my sails project on a server. I installed mysql.
When i call the request to create a user, sails doesn't automatically create the table "User".
I get this error:
sails-hook-sockets :: connected to Sails admin message bus.
Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_NO_SUCH_TABLE: Table 'mooz.user' doesn't exist
at Query.Sequence._packetToError (/home/mooz/api/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
at Query.ErrorPacket (/home/mooz/api/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)
at Protocol._parsePacket (/home/mooz/api/node_modules/mysql/lib/protocol/Protocol.js:271:23)
at Parser.write (/home/mooz/api/node_modules/mysql/lib/protocol/Parser.js:77:12)
at Protocol.write (/home/mooz/api/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/mooz/api/node_modules/mysql/lib/Connection.js:92:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:710:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)
at emitReadable (_stream_readable.js:378:5)
at readableAddChunk (_stream_readable.js:143:7)
at Socket.Readable.push (_stream_readable.js:113:10)
at TCP.onread (net.js:511:21)
--------------------
at Protocol._enqueue (/home/mooz/api/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at PoolConnection.Connection.query (/home/mooz/api/node_modules/mysql/lib/Connection.js:197:25)
at __FIND__ (/home/mooz/api/node_modules/sails-mysql/lib/adapter.js:836:20)
at afterwards (/home/mooz/api/node_modules/sails-mysql/lib/connections/spawn.js:84:5)
at /home/mooz/api/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Ping.onPing [as _callback] (/home/mooz/api/node_modules/mysql/lib/Pool.js:94:5)
at Ping.Sequence.end (/home/mooz/api/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Ping.Sequence.OkPacket (/home/mooz/api/node_modules/mysql/lib/protocol/sequences/Sequence.js:105:8)
at Protocol._parsePacket (/home/mooz/api/node_modules/mysql/lib/protocol/Protocol.js:271:23)
at Parser.write (/home/mooz/api/node_modules/mysql/lib/protocol/Parser.js:77:12)
at Protocol.write (/home/mooz/api/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/mooz/api/node_modules/mysql/lib/Connection.js:92:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:710:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)
at emitReadable (_stream_readable.js:378:5)
at readableAddChunk (_stream_readable.js:143:7)
at Socket.Readable.push (_stream_readable.js:113:10)
at TCP.onread (net.js:511:21)
Details: Error: ER_NO_SUCH_TABLE: Table 'mooz.user' doesn't exist
Here my model User.js
var Waterline = require('waterline');
// Define your collection (aka model)
var User = Waterline.Collection.extend({
attributes: {
connection:'mysql',
tableName:'user',
id: {
type: 'string',
required: true,
primaryKey: true,
unique: true
},
username: {
type: 'string',
required: true
}
}
});
My model.js:
module.exports.models = {
connection: 'mysql',
migrate: 'safe'
};
My connection.js:
module.exports.connections = {
mysql: {
adapter : 'sails-mysql',
host : 'localhost',
user : 'user',
port : 3306,
password : 'test',
database : 'mooz'
}
};
And my userController
module.exports = {
createUser: function(req, res){
var params = req.params.all();
User.create({username: params.username, id: params.id}).exec(function createUser(err,created){
return res.json({
notice: 'Created user ' + params.username
});
});
}
};
I don't understand why sails can't create automatically the table, did i forget something? Is there any command to do?
Thanks.
EDIT: Resolved with dev environnement
You can try to set migrate: 'alter' in model.js, and sails lift again.