1

i want to replace all < in a string with &lt and insert it into MongoDB.
But when i insert it, MongoDB automatically replace &lt with <.
How can i do to prevent it?
This is the code for replace all < with the escaping quote:

let desc;

splitDesc.forEach(element => {
   if(element == "<") {
        desc += "&lt";
   } else if(element != "<") {
        desc += element;
   }
});
6
  • 1
    &amp would never be replaced with <. I could see &lt getting converted to <, or &amp converted to &, but that makes no sense. You probably have a different error Commented Jun 4, 2021 at 17:13
  • @GarrGodfrey oh yes sorry. I have the same problem with &lt. Commented Jun 4, 2021 at 17:28
  • 1
    you may need to show more code, like your actual update. I'm concerned by your else statement, since the if is unnecessary. Is it possible the &lt is converted to < only when you view it, if you are viewing in a web browser? Commented Jun 4, 2021 at 17:37
  • 1
    since desc starts out as undefined, it will still be undefined even after appending to it. So this code actually does nothing. Commented Jun 4, 2021 at 17:38
  • 1
    @GarrGodfrey I think it'd just coerce undefined to a string; you'd end up with a string prefixed by "undefined" plus everything else appended to it. Commented Jun 4, 2021 at 17:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.