guagay_wk
guagay_wk

Reputation: 28030

node.js mysql query cannot run due to quotes around digit

I have this mysql query written in node.js mysql and restify to be executed in a HTTP GET. https://github.com/felixge/node-mysql

var api_get_func = function (app, url_path) {
    function respond(req, res, next) {
        var id= req.query.id;
        var limit = req.query.limit;   

    var query_str =
        "SELECT table.sw_type, " +
        "FROM users " +
        "WHERE (id = ?) " + 
        "LIMIT ?"; //limit

    var var_arr = [id, limit];  

    var query = connection.query(query_str, var_arr, function (err, rows, fields) {}
        //SQL query ...
        return next();
    }

app.get(url_path, respond);
}

The HTTP GET URL is http://127.0.0.1/read_sw?id=51&limit=1

The error reported is that the query is not properly formed. The actual query looks like this;

SELECT table.sw_type, 
            FROM users 
            WHERE (id = 51) 
            LIMIT '1'

I think the problem is LIMIT '1'. I think the solution is to make the query look like this;

SELECT table.sw_type, 
            FROM users 
            WHERE (id = 51) 
            LIMIT 1

How can the node.js code be modified?

Upvotes: 1

Views: 790

Answers (2)

Atul Agrawal
Atul Agrawal

Reputation: 1520

You can also use

var limit = Number(req.query.limit);  

Upvotes: 1

sebilasse
sebilasse

Reputation: 4618

This is not about mysql...

Anything in req.query is a string :

How about

var var_arr = [id, parseInt(limit)];

Upvotes: 2

Related Questions