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
Test
"" __proto__: Object blog_id: 7 edit: true editable: true ` :::browser output – Altuğ Ceylan Oct 06 '20 at 14:27