node-mysql

Node MySQL escape LIKE statement

梦想与她 提交于 2019-11-29 02:56:58
How do escape a MySQL LIKE statement in node-mysql? Something along the lines of "SELECT * FROM card WHERE name LIKE '%" + connection.escape(req.body.search) + "%'" Results in 'SELECT * FROM card WHERE name LIKE \'%\'hello\'%\'' Which is a syntax error. If I use the alternative syntax of connection.query("SELECT * FROM card WHERE name LIKE '%?%'", req.body.search, function () {}); Results in a similar syntax error. I've also tried connection.query("SELECT * FROM card WHERE name LIKE ?", '%' + req.body.search + '%', function () {}); Which just ends up escaping the '%' sign. Not sure why it's

Reproduce MySQL error: The server closed the connection (node.js)

混江龙づ霸主 提交于 2019-11-28 22:50:06
问题 I'm trying to reproduce a MySQL error I'm seeing in my node.js app on EC2 with the node mysql library: Connection lost: The server closed the connection. I am unable to reproduce the error locally- killing the database is handled just fine by my code- it just rechecks every few seconds and reconnects to the db once it is restarted. On EC2, it happens around 4am Pacific, but the db is still up and running fine. I'd like to Reproduce the crash with my local mysql Add whatever logic I need in my

Node.js MySQL Needing Persistent Connection

不羁岁月 提交于 2019-11-28 16:32:11
I need a persistent MySQL connection for my Node web app. The problem is that this happens about a few times a day: Error: Connection lost: The server closed the connection. at Protocol.end (/var/www/n/node_modules/mysql/lib/protocol/Protocol.js:73:13) at Socket.onend (stream.js:79:10) at Socket.EventEmitter.emit (events.js:117:20) at _stream_readable.js:895:16 at process._tickCallback (node.js:415:13) error: Forever detected script exited with code: 8 error: Forever restarting script for 2 time info: socket.io started Here is my connection code: // Yes I know multipleStatements can be

Undefined value after returning an array of values from a MySQL query in a different file

旧城冷巷雨未停 提交于 2019-11-28 14:49:46
I'm using the Mysql connector because I need to get some data from my database, but I'm having the following issue: I have two files server.js and dbConnection.js and I have a return in the dbConnection.js file which should return an array and it should show it in the server.js file. However, it prints out an undefined array. I don't understand what I'm doing wrong since I also tried to print out the array before reurning it in the dbConnection.js file and it's shown with the data. server.js: const express = require('express'); const dbConnection = require('./dbConnection.js'); app.get('/',

node.js, express - executing mysql queries one after another within loops in a synchronous way

一个人想着一个人 提交于 2019-11-28 10:10:30
In my node.js, express app, I am making an ajax call with the superagent middleware. The call fetches database data in a complex array using the node-mysql middleware through quite a few database queries. Before pasting the code, I am trying to explain in words what I am trying to do although the code would suffice to say what it wants to do with the addition that all the asynchronous things inside the first callback should be done in the synchronous way. Explanation: Inside the callback of the first query , a for loop is executed to run the second query multiple times and after each loop, the

Use promise to process MySQL return value in node.js

ぃ、小莉子 提交于 2019-11-28 05:22:45
I have a python background and is currently migrating to node.js. I have problem adjusting to node.js due to its asynchronous nature. For example, I am trying to return a value from a MySQL function. function getLastRecord(name) { var connection = getMySQL_connection(); var query_str = "SELECT name, " + "FROM records " + "WHERE (name = ?) " + "LIMIT 1 "; var query_var = [name]; var query = connection.query(query_str, query_var, function (err, rows, fields) { //if (err) throw err; if (err) { //throw err; console.log(err); logger.info(err); } else { //console.log(rows); return rows; } }); //var

How to input a NodeJS variable into an SQL query

我怕爱的太早我们不能终老 提交于 2019-11-28 01:28:44
问题 I want to write an SQL query that contains a NodeJS variable. When I do this, it gives me an error of 'undefined'. I want the SQL query below to recognize the flightNo variable. How can a NodeJS variable be input into an SQL query? Does it need special characters around it like $ or ? . app.get("/arrivals/:flightNo?", cors(), function(req,res){ var flightNo = req.params.flightNo; connection.query("SELECT * FROM arrivals WHERE flight = 'flightNo'", function(err, rows, fields) { 回答1: You will

How to properly pass mysql connection to routes with express.js

坚强是说给别人听的谎言 提交于 2019-11-27 18:23:07
I am trying to figure out the best way to pass a mysql connection (using node-mysql) between my routes for express.js. I am dynamically adding each route (using a for each file loop in routes), meaning I can't just pass in the connection to routes that need it. I either need to pass it to every route or none at all. I didn't like the idea of passing it to ones that dont need it so I created a dbConnection.js that the routes can individually import if they need. The problem is that I dont think I am doing it correctly. As of now, my dbConnection.js contains: var mysql = require('mysql'); var db

Node.js MySQL Error Handling

淺唱寂寞╮ 提交于 2019-11-27 16:07:44
问题 I've read several examples for using mysql in node.js and I have questions about the error handling. Most examples do error handling like this (perhaps for brevity): app.get('/countries', function(req, res) { pool.createConnection(function(err, connection) { if (err) { throw err; } connection.query(sql, function(err, results) { if (err) { throw err; } connection.release(); // do something with results }); }); }); This causes the server to crash every time there's an sql error. I'd like to

MySQL giving “read ECONNRESET” error after idle time on node.js server

烂漫一生 提交于 2019-11-27 15:08:32
I'm running a Node server connecting to MySQL via the node-mysql module. Connecting to and querying MySQL works great initially without any errors, however, the first query after leaving the Node server idle for a couple hours results in an error. The error is the familiar read ECONNRESET , coming from the depths of the node-mysql module. A stack trace (note that the three entries of the trace belong to my app's error reporting code): Error at exports.Error.utils.createClass.init (D:\home\site\wwwroot\errors.js:180:16) at new newclass (D:\home\site\wwwroot\utils.js:68:14) at Query._callback (D