0

I'm using Node.js with PostgreSQL using Sequelize as ORM and I have objects that have props where the string contains \u0000.
Now PostgreSQL throws an error \u0000 cannot be converted to text. when I try to write object containing that to the database.
As a quick fix I have tried to do replaceAll("\u0000", "") on all string props of the object before database method, however the error still occurs.
Does anyone have an idea of what is happening here. Could it be that Node isn't recognising \u0000 and hence not replacing it? Should I maybe use regex in replaceAll

hatch
  • 49
  • 3
  • Does this answer your question? [Handling Unicode sequences in postgresql](https://stackoverflow.com/questions/31671634/handling-unicode-sequences-in-postgresql) – Harsha Biyani May 04 '23 at 10:36
  • Not really, they are trying to query the db directly, but I need this to replace all \u0000 before database write method. – hatch May 04 '23 at 10:41

1 Answers1

0

If you want to delete exactly \u0000, try this:

function removeNullBytes(str){
  return str.split("").filter(char => char.codePointAt(0)).join("")
}
console.log(removeNullBytes("MyString\u0000\u0000\u0000"))

Original source: Removing a null character from a string in javascript

General Grievance
  • 4,555
  • 31
  • 31
  • 45
McSterka
  • 1
  • 2