Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
In my Node js, I have escaped single quote with the below function
var regescape = function(text) { return text.replace(/[\[\]']+/g, "\\$&"); };
This is working fine for me. But suddenly I discovered I have a string M'$ in my database. Which is not returning with my below query.
param 1 = "M'$"; var cursor = db.collection('search').find({"searchcontent.name":new RegExp('^'+regescape(param1))}).limit(10);
Also Please suggest the best practice for handling Node JS parameter pass to MongoDB. I am calling NodeJS from PHP code. And I am sending parameters with rawurlencode() from PHP code. In node js I'm using decodeURI() to the received parameters.
rawurlencode()
decodeURI()
Edit:
My PHP code for calling Node JS:
function getdetail($data1) { $p1 = $data1; $service_url = 'http://exampleserver:8081/search?param1='.$p1; $curl = curl_init($service_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $curl_response = curl_exec($curl); if ($curl_response === false) { $info = curl_getinfo($curl); curl_close($curl); die('error occured.Please try later'); } curl_close($curl); $decoded = json_decode($curl_response, true); if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { die('error occured.Please try later'); } return $decoded; }
Node JS code to receive data:
app.get('/search', function (req, res) { var param1=decodeURI(req.query.param1); MongoClient.connect(url, function(err, db) { assert.equal(null, err); search(param1,db, function(data){ db.close(); res.end
The problem is with $ sign. It's a special character and it should be escaped, because normally it means end of input. You have to update regescape function, because it escapes only single quote and square brackets - so you have at least add dollar sign there too.
regescape
Also seems like your regescape function does not work as expected in some cases. Try for example to pass this value: test[]'. I think you expect to get test\[\]\', but actually you will get test\[]'
test[]'
test\[\]\'
test\[]'
So in order to fix it and add dolar sign - it should be something like this:
var regescape = function(text) { return text.replace(/'|\$|\[|\]/g, "\\$&"); };
Pipe (|) means or, so it simply escapes any of the symbols from the set. You can easily add more characters there in future.
or
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.