Here is a line from my node js code:
var qry = 'INSERT INTO "sma"."RMD"("UserId","Favourite") VALUES (' + req.body.user + ',' + JSON.stringify(req.body.favourite) + ')'
I want to insert single quotes before JSON.stringify(req.body.favourite)
. But i'm unable to escape the single quotes. I can't debug the issue since I'm uploading the code to a predix cloud server
Any idea how to achieve that?
You could also use template literals to build your query
Template literals which use the back-tick character Check main answer here
alert(`Use "double" and 'single' quotes in the same string`);
alert(`The escape the \` back-tick character in a string`);
Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.
Why don't you use prepared statements?
var qry = 'INSERT INTO "sma"."RMD"("UserId","Favourite") VALUES ($1, $2)';
client.query(qry, [ req.body.user, JSON.stringify(req.body.favourite])'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With