1

I have two strings. One is constant and another comes from firebase firestore. both are the same strings.

First one is local string:

const str =
              "3\r\n4 9\r\n2 7 11 15\r\n5 4\r\n2 3 1 15 7\r\n4 31\r\n16 1 15 7";

And another is string from firebase firestore:

{
    str: "3\r\n4 9\r\n2 7 11 15\r\n5 4\r\n2 3 1 15 7\r\n4 31\r\n16 1 15 7",
}

When I try to compare these two I get false output to console:

console.log(str === docSnapshot.data().str); // output: false

When I log both strings to console I get this output.

For first one:

console.log(str);

**output-**
3
4 9
2 7 11 15
5 4
2 3 1 15 7
4 31
16 1 15 7

For firestore sting:

console.log(docSnapshot.data().str);

**output-**
3\r\n4 9\r\n2 7 11 15\r\n5 4\r\n2 3 1 15 7\r\n4 31\r\n16 1 15 7

    

Please help me understanding this. And how can I log firebase string to console same as first one?

Chandragupt
  • 11
  • 1
  • 1
  • hi, is it possible the newline chars were escaped before inserting into firebase? If so, you might need to `str.replaceAll('\\r\\n', '\r\n')` to convert the escape sequences back into newlines. – jspcal Apr 11 '22 at 18:41
  • I have entered data manually into firebase. I think you are correct, those characters are being escaped while inserting. Do you have any idea how to avoid this while inserting into firebase? – Chandragupt Apr 11 '22 at 18:49
  • Instead of copying the raw string, copy what it prints when you are manually entering into firebase. When you programmatically enter it into firebase, you should not have to worry about it escaping the string you pass. \r\n is a carriage return, new line character. It will look just like a newline when you print it. – async await Apr 11 '22 at 18:54
  • Copying printed and inserting manually doesn't seem to work. It is treating string as single line string like this 3 4 9 2 7 11 15 5 4 2 3 1 15 7 4 31 16 1 15 7 and not multi line. Thanks for suggestion though – Chandragupt Apr 11 '22 at 18:59
  • https://stackoverflow.com/questions/46800639/how-to-create-newline-when-retrieving-data-from-firebase-database-into-textview – James Apr 11 '22 at 19:21

0 Answers0