I am trying to filter some data by “averageRating”.
This is my code for the method:
filterr(request, respond) { var averageRating = request.params.rating; var sql = "SELECT * FROM shopreview.shops WHERE averageRating = ?"; db.query(sql, [averageRating], function (error, result) { if (error) { throw error; } else { respond.json(result); } }); }
My sql statement is working when I test it against my database. However, I keep getting [] as my result. Can someone please help identify what the problem is? Thanks a lot!
Advertisement
Answer
the problem is that “?” since the db is unable to parse it.
either add that avarageRating variable like so:
var sql = "SELECT * FROM shopreview.shops WHERE averageRating = ${parseInt(avarageRating)}";
or if you’re using couchbase you could parse it like this:
var sql = `SELECT * FROM shopreview.shops WHERE averageRating = $1`;
where $1 is the first variable in the array of variables.