Darian R.
Darian R.

Reputation: 31

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 => "[email protected]" => "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.

Upvotes: 0

Views: 56

Answers (1)

Darian R.
Darian R.

Reputation: 31

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)

};

Upvotes: 1

Related Questions