-1

I am trying to send some objects to client using ejs. I haven't had any problems while doing so, so far at least. I store quills like this

{"form_type":"blog","form_sub_type":"create","blog_title":"Test","quill_0":"{\"ops\":[{\"insert\":\"tesarfd\\n\"}]}"}

When I try to send them to client first I run this function to get the object from file

const fileToJson = async (filePath) => {
    return new Promise(resolve => {
        fs.readFile(filePath, 'utf-8', (err, data) => {
            if (err) {
                console.log(err);
                resolve(false);
            }
            resolve(data);//returns json string 
        })
    })
}

At client, I tried using the following:

'<%-JSON.stringify(blog)%>'
'<%-blog%>'

When I logged the second one I only got [Object object] and couldn't access its fields. When I logged the first one I got:

{"edit":true,"editable":true,"blog_id":3,"blog":{"form_type":"blog","form_sub_type":"create","blog_title":"Test","quill_0":"{"ops":[{"insert":"tesarfd\n"}]}"}}

and cannot parse it.

Code that produces the error:

 const blog_info = '<%-JSON.stringify(blog)%>';
    console.log(JSON.parse(blog_info));

Error:

Uncaught SyntaxError: Unexpected token f in JSON at position 51
    at JSON.parse (<anonymous>)
    at blog_panel?id=6:335

Edit2: the line from source with another string that produces the same error

const blog_content=JSON.parse('{"edit":true,"editable":true,"blog_id":7,"blog":"{\"form_type\":\"blog\",\"form_sub_type\":\"create\",\"blog_title\":\"Test\",\"quill_0\":\"\\\"<p>Test</p>\\\"\"}"}');

JSON.parse() throws:

Uncaught SyntaxError: Unexpected token o in JSON at position 3
    at JSON.parse (<anonymous>)
    at blog_panel?id=6:335
Altuğ Ceylan
  • 93
  • 1
  • 10
  • 1
    I find the question a bit confusing. The title of the question says *"JSON.parse() error"* So I assume it is about the line `resolve(JSON.parse(data.toString()));` However you don't provide what the error is. Could you add the error you encounter to the question? – 3limin4t0r Oct 06 '20 at 12:35
  • @3limin4t0r edited accordingly – Altuğ Ceylan Oct 06 '20 at 12:43
  • *I only got [Object object]*, yes this is correct? What were you expecting it to do? JSON.Parse returns an object?! If you want a string. Don't parse it. – Liam Oct 06 '20 at 14:17
  • I am trying to parse a nested json object that's passed as a string to client – Altuğ Ceylan Oct 06 '20 at 14:21
  • You are almost there. Looking at your "edit2" you need to get rid of the `JSON.parse(...)` call and the surrounding single quotes. The output should be `const blog_content = {"edit":true, ...}` – 3limin4t0r Oct 06 '20 at 14:23
  • @3limin4t0r I love you man <3, I will not use webstorm again after this ffs. It worked. `{edit: true, editable: true, blog_id: 7, blog: {…}} blog: blog_title: "Test" form_sub_type: "create" form_type: "blog" quill_0: ""

    Test

    "" __proto__: Object blog_id: 7 edit: true editable: true ` :::browser output
    – Altuğ Ceylan Oct 06 '20 at 14:27
  • @AltuğCeylan What did you change to make it work? Did my answer help? If the actual solution is different from what I wrote, consider writing an answer to your own question. – 3limin4t0r Oct 06 '20 at 14:29
  • @3limin4t0r I used const blog_content=<%-JSON.stringify(blog)%>; for some reason I didn't get a console output earlier even though I saw it in sources, now I did. I also changed how I write quill to json, instead of JSON.stringify(quill.getContents()), I used JSON.stringify(quill.root.innerHTML) not sure if that made a difference though. – Altuğ Ceylan Oct 06 '20 at 14:33

2 Answers2

2

data.toString() will return "[Object object]", so that is all you will get out of the resolve. You should call stringify there instead, that will probably help.

Luke Storry
  • 6,032
  • 1
  • 9
  • 22
  • Still cannot parse the following string resulted from changing data.toString() to JSON.stringify(data) : `{"edit":true,"editable":true,"blog_id":6,"blog":"{"form_type":"blog","form_sub_type":"create","blog_title":"fdsafasdf","quill_0":"{\"ops\":[{\"insert\":\"fsdafsda\\n\"}]}"}"}` – Altuğ Ceylan Oct 06 '20 at 12:38
  • That's not valid JSON. Put it into jsonlint.com and you'll see that there is an extra set of quotation marks that messes it up. The other answer that takes that bit out of the string is what you're looking for in this case :) – Luke Storry Oct 06 '20 at 14:44
  • thank you, seems like I messed up big time – Altuğ Ceylan Oct 06 '20 at 15:00
1

The problem with:

const blog_info = '<%-JSON.stringify(blog)%>';
console.log(JSON.parse(blog_info));

Is that <%-JSON.stringify(blog)%> is placed within string context (single quotes in this case). Let me give you an example that demonstrates the issue.

// example #1
// assumption
<% blog = {title: "Let's play Tic-Tac-Toe"} %>

// Then this will result in:
const blog_info = '<%-JSON.stringify(blog)%>';
const blog_info = '{"title":"Let's play Tic-Tac-Toe"}';
// Uncaught SyntaxError: unexpected token: identifier
// example #2
// assumption
<% blog = {title: "foo\nbar"} %>

// Then this will result in:
const blog_info = '<%-JSON.stringify(blog)%>';
const blog_info = '{"title":"foo\nbar"}';
console.log(JSON.parse(blog_info));
// Uncaught SyntaxError: JSON.parse: bad control character in string literal at line 1 column 14 of the JSON data

Instead of placing the result in string context assign it directly to the variable. JSON stands for JavaScript Object Notation and is compatible with normal JavaScript*. This means changing the code to:

const blog_info = <%-JSON.stringify(blog)%>;
console.log(blog_info);

Using the same examples as above this results in:

// example #1
// assumption
<% blog = {title: "Let's play Tic-Tac-Toe"} %>

// Then this will result in:
const blog_info = <%-JSON.stringify(blog)%>;
const blog_info = {"title":"Let's play Tic-Tac-Toe"};
console.log(blog_info);
// prints: ⏵ Object { title: "Let's play Tic-Tac-Toe" }
// example #2
// assumption
<% blog = {title: "foo\nbar"} %>

// Then this will result in:
const blog_info = <%-JSON.stringify(blog)%>;
const blog_info = {"title": "foo\nbar"};
console.log(blog_info);
// prints: ⏵ Object { title: "foo\nbar" }
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • my ide displays error at angle brackets when I write `const blog_info=<%-JSON.stringify(blog)%>;` and when I try to log it, ide warns that blog_info might not be initialized, outputs nothing to console. I am writing the code inside tags – Altuğ Ceylan Oct 06 '20 at 13:34
  • What is the error the IDE displays? If nothing outputs to the console it might be wise to check the generated page (the page that the client receives). Have you done this already? Do you see anything unusual or different from expectations? – 3limin4t0r Oct 06 '20 at 13:52
  • I have been checking via browser console if that's what you mean – Altuğ Ceylan Oct 06 '20 at 13:55
  • That's not what I mean. Checking the console output is already one step to far. EJS is a templating language which you can use to generate output (eg. HTML/JavaScript/CSS etc.). My question is if the output is correct or unusual. In most browsers you can press Ctrl + U to view the page source. If the JavaScript is not part of the current page but is loaded through a `` tag you can either check the JavaScript debugger for loaded JavaScript files, or the network tab that shows what external JavaScript is loaded and what its content is. – 3limin4t0r Oct 06 '20 at 14:09
  • oh yes, it was an ide related thing, browser seems to display it fine in sources. Not sure why I can't log it though. – Altuğ Ceylan Oct 06 '20 at 14:24