node-mysql

Updating multiple rows with node-mysql, NodeJS and Q

╄→尐↘猪︶ㄣ 提交于 2019-11-30 13:55:35
I am using node-mysql, node-js, and Q promises. I have successfully updated, deleted, and inserted single rows using the above. As well as inserted multiple rows in a single statement in my test case scenario. However, I need to update multiple rows with different values (batch mode) either in a single query or in a for loop. The information on how to use prepared statements in mysql2 (supposed to improve on node-mysql) is very sparse and there are no examples, although that should be the natural choice, together with promises to compensate for node-js asynchronous nature. In addition, I have

Node.js MySQL Error Handling

∥☆過路亽.° 提交于 2019-11-30 11:43:55
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 avoid that and keep the server running. My code is like this: app.get('/countries', function(req, res) {

In node.js mysql query, check if no matching found

心不动则不痛 提交于 2019-11-30 08:52:28
How can I check if no matching found in mysql (node.js)? mysql.query("select * from table1 where name = 'abcd'", function(error, result, field) { if(error) { exist(error); //No error } else if(result) { console.log(result); //displays '[]' exist(null, result); } else { exist(null, null); //It is never execute } }); function exist(error, result) { if(result) console.log("Test:"+result); //Executed and displays 'Test:' } My database does not contains name = 'abcd'. So, how can I check if the query does not match? Rodrigo Medeiros You're receiving an empty array ( [] ) as result of your query,

How to return a value from a mysql SELECT query in node.js

纵然是瞬间 提交于 2019-11-30 07:35:37
I'm still very new to Node.js, and i'm trying to understand how callbacks work. So, here is my problem : I should've put more code : POST : app.post('/register', function(req, res) { //get data from the request var data = { username: req.body.username, email: req.body.email, password: req.body.password }; function fetchID(callback) { connection.query('SELECT id_user FROM USERS WHERE username = ?', data.username, function(err, rows) { if (err) { callback(err, null); } else callback(null, rows[0].id_user); }); } var user_id; fetchID(function(err, content) { if (err) { console.log(err); return

Node MySQL escape LIKE statement

旧时模样 提交于 2019-11-30 07:24:44
问题 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 ?", '%' +

Node.js mysql transaction

耗尽温柔 提交于 2019-11-30 07:20:08
Can anyone provide an example of how I could achieve MySQL transactions in Node.js. I am trying to get my head around using the node-mysql driver and node-mysql-queue. As far are I can tell, using node-mysql-queue greatly reduces the asynchronous nature of Node.js as new queries have to wait until existing ones have completed. To get around this, has anyone attempted to combine node-mysql-queue with node-mysql's connection-pooling capabilities. i.e starting a new mysql connection for each new http request, and starting transaction queues on individual connections? The following transaction

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

浪子不回头ぞ 提交于 2019-11-30 01:57:25
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 mysql helper module to handle this Here's the error in my node.js app: 2012-10-22T08:45:40.518Z -

In node.js mysql query, check if no matching found

扶醉桌前 提交于 2019-11-29 07:49:54
问题 How can I check if no matching found in mysql (node.js)? mysql.query("select * from table1 where name = 'abcd'", function(error, result, field) { if(error) { exist(error); //No error } else if(result) { console.log(result); //displays '[]' exist(null, result); } else { exist(null, null); //It is never execute } }); function exist(error, result) { if(result) console.log("Test:"+result); //Executed and displays 'Test:' } My database does not contains name = 'abcd'. So, how can I check if the

How to input a NodeJS variable into an SQL query

泪湿孤枕 提交于 2019-11-29 07:44:30
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) { You will need to put the value of the variable into the SQL statement. This is no good: "SELECT * FROM arrivals WHERE

Express JS 'this' undefined after routing with app.get(..)

瘦欲@ 提交于 2019-11-29 03:56:54
I have a basic Node JS server which is designed to be used as an API, I've created a log and database module and I've started adding other modules to deal with different request types. I'm using Express.js and node-mysql When I visit /v1/group I get the following error - TypeError: Cannot read property 'database' of undefined at Group.getAll (C:\code\javascript\node\api\api\v1\groups.js:12:23) at callbacks (C:\code\javascript\node\api\node_modules\express\lib\router\index.js:161:37) ... So I guess after recieving a request and calling group.getAll() that this is undefined but I don't