0

Why do i get unexpected end of JSON input at JSON.parse <anonymous> error? [unsolved]

Here is the code that produces the error ,also i am not sure why content length is logged out as 1 ,maybe I don't understand how this method really works ,so pls help me clear my doubts,thanks in advance const content=" " req.on('data',(chunk)=>{ content+=chunk; }) req.on("end",()=>{ console.log(content.length); console.log(content,JSON.parse(conten t)); });

17th Aug 2020, 4:00 PM
Abhay
Abhay - avatar
6 ответов
+ 4
You have extra two brackets at the end i think. And a constant can't be changed so use let instead of const.
17th Aug 2020, 4:03 PM
maf
maf - avatar
+ 2
Try console log without json parse and see what you try to convert and see if there is allready a bug there
17th Aug 2020, 4:07 PM
Dragonxiv
Dragonxiv - avatar
+ 1
maf there is no extra brackets ,also let ,const and var nothing works
17th Aug 2020, 4:09 PM
Abhay
Abhay - avatar
+ 1
Dragonxiv when I simply remove all statements and just do console.log(content) then I get {"message":"hii"} which is the message I am sending but when I do , console.log(content.length) console.log(content,JSON.parse(content)); Here the content is just " " whose length is one and it can't be parsed ,
17th Aug 2020, 4:19 PM
Abhay
Abhay - avatar
+ 1
Dragonxiv yes but json.parse on content doesn't works strangely as from some stackoverflow question and my own assumption content is just " " instead of message:hi when parsed ,I am not sure but if this isn't reason I am not able to understand why I get the above error
17th Aug 2020, 9:45 PM
Abhay
Abhay - avatar
0
And you put the console.log inside the req.on('end',()=> { here ?})
17th Aug 2020, 9:32 PM
Dragonxiv
Dragonxiv - avatar