简体   繁体   中英

Unable to query postgres db with _ using javascript

I'm trouble shooting this application that I ave had no hand in creating, but I get to fix. Anywho, I'm finding that I am unable to query the postgres db for any variable that has an _. Now before you say use escape characters, I've already been there, done that, and got the t-shirt. From what I could tell, the application uses escape-string-regexp module to alter the string, for example => "some_random@email.com" => "some_random@email\\.com" I added my own module to single out the _ and now the output is => "some\\\\_random@email\\.com". Note the two escapes for the underscore as prescribed by the mass forums that I have scaled. I even built a tool to test out in python and that works. Slightly different as I use => "(E'some\\\\_random@email\\\\.com')". Tried that with javascript as well and still no dice. What say all ye? Oh, this shoots out via expressjs from what I can tell. I'm not a javascript guy.

Found the solution. I and another team mate went back and revisited the method I created and changed the " to '. Next thing you know, it started working.

exports.stringChanger = function(word){
//console.log(word)
var metas = ["_"];
var guestPool = word.split("");

//console.log(guestPool)

for(i in range(guestPool.length)){
    for(j in range(metas.length)){
        if(guestPool[i] === metas[j]){
            guestPool[i] = "\\" + guestPool[i];
        }
    }
}

var guest = guestPool.join("");
return guest;
//console.log(guest)

};

to

exports.stringChanger = function(word){
//console.log(word)
var metas = ['_'];
var guestPool = word.split("");

//console.log(guestPool)

for(i in range(guestPool.length)){
    for(j in range(metas.length)){
        if(guestPool[i] === metas[j]){
            guestPool[i] = '\\' + guestPool[i];
        }
    }
}

var guest = guestPool.join("");
return guest;
//console.log(guest)

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM