0

I have a url like this ... where multiple (comma seperated values can be added to the url)

localhost:4001/api/v1/users/search?title=mr,dr  

That's my query:

router.get('/search?', function(req, res, next) {
    var title = req.query.title;

    var paramTitle = '\'' + title.split(',').join('\',\'') + '\''; 

    var setParams = [];
    if (title) {    
        setParams = [paramTitle];
    } 

    var sql = "SELECT * from users WHERE title IN (?)"; 

    connection.query(sql, setParams, function (error, results, fields) {
        res.json({"status": 200, "error": null, "response": results});
    });
});

But I get an Error:

sql "SELECT * from users WHERE title IN '(\\'mr\\',\\'dr\\')' LIMIT 100"

How can I get this:

var sql = "SELECT * from users WHERE title IN ('mr','dr')";

1 Answer 1

4

The binding is expecting an array, so you don't need to do the manual joining yourself.

var paramTitle = title.split(','); 

var setParams = [];
if (title) {    
    setParams = [paramTitle];
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.