nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Error in JSON.parse() NodeJs #4371

Closed FranciscoCastle closed 2 months ago

FranciscoCastle commented 2 months ago

Version

21.7.1

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

image

let data = JSON.stringify({string: "example text", object: {string: "text 2",object: {string: 3}}});
console.log(JSON.parse(data));
let _data = {data:JSON.parse(data)};
console.log(_data);

How often does it reproduce? Is there a required condition?

All the time I run my app in nodejs

in my browser console it works correctly:

image

What is the expected behavior? Why is that the expected behavior?

{
  string: 'example text',
  object: { string: 'text 2', object: { string: 3 } }
}
{
  data: {
    string: 'example text',
    object: { string: 'text 2', object: { string: 3 } } // not [Object]
  }
}

What do you see instead?

result in console: image

{
  string: 'example text',
  object: { string: 'text 2', object: { string: 3 } }
}
{
  data: {
    string: 'example text',
    object: { string: 'text 2', object: [Object] }
  }
}

Additional information

I would like to send an object to my view in Pug but when reading it in Pug I only get this in the view:

image

index.js:

res.render('index',_data);

index.pug:

p= data
kylo5aby commented 2 months ago

I'm happy to fix it

climba03003 commented 2 months ago

I'm happy to fix it

Shouldn't it just display issue? Since REPL or console.log do not like browser provide a way to expend each property, the display of [Object] still means it is a proper object.

https://nodejs.org/api/util.html#utilinspectobject-showhidden-depth-colors The maximum depth of console.log show is 2.

import pug from 'pug'

const html = pug.render('p= data', {
  data: {
    string: 'example text',
    object: { string: 'text 2', object: { string: 3 } } // not [Object]
  }
})

console.log(html) // <p>[object Object]</p>

pug is actually printing the object using .toString and that is expected result.

kylo5aby commented 2 months ago

Sounds reasonable, the default depthofconsole.logis 2, that's why{ string: 3 }` doesn't been shown, but I haven't found any doc to describe it.

climba03003 commented 2 months ago

but I haven't found any doc to describe it.

console.log said it using util.format. image

util.format said it using util.inspect for object. image

util.inspect is telling you depth is 2 by default. image

aduh95 commented 2 months ago

It has nothing to do with JSON.parse(), as @climba03003 mentioned it's the documented behavior of console.log(). You should first convert the data to a string with e.g. JSON.stringify(), util.format(), or a third party library.

aliakbar192 commented 2 months ago

In your code, you're correctly stringifying an object and then parsing it back into an object. However, when you pass _data to your Pug template, you're assigning the entire object to a variable called data. This is why you're seeing [object Object] in your view because data is an object, and Pug is displaying it as a string representation.

RedYetiDev commented 2 months ago

(This issue seems resolved)