'Posting a single quote into a Postgres database

I am currently trying to take a string submitted from a form and make an api call to submit the message into the Postgres data base. Everything works sucessfully except when dealing with single quotes. The simplified code is:

const message = form.get('message').replace(/\'/g, "E\'\'")
urlData = ...&reasoning=${message}
methodApi = ...

axios.get(methodApi + urlData);

I am currently using the .replace function above to follow the format when dealing with single quotes. And it works but the message submitted to the database has the E present

example value from db after submission:

testing single quote: E'

When I do not use the .replace function the api gets cut off and wont submit, also just replacing a single quote with \' does not work and two single quotes does not work either.

So basically I've made progress and am trying to have the "E" not show up in the db.

Most likely a simple fix but each time I push the code to the dev environment to test the db it takes about 20 minutes, so asking here while I work on it will save some time.

Thanks and let me know if this is confusing I will be sure to respond asap!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source