Home > Enterprise >  How to parse [object Object]?
How to parse [object Object]?

Time:04-22

I try to get data from a valid file and it return [object Object] so I can't get any value from it.

const data = require('./path/to/file');
console.log(data, data.id);

Data file

module.exports = {
  id: 1,
  name: 'fox',
  rate: 10
}

or json data file

{
  "id": 1,
  "name": "fox",
  "rate": 10
}

Console:

[object Object]
undefined

I try to use JSON.parse(data) but it still like that

CodePudding user response:

That's expected because you are logging in nodejs console, which behaves differently than browser console. If you wish to be able to log full objects in nodejs console, you can use

console.log(JSON.strinfigy(data))

check more about it here: https://nodejs.dev/learn/how-to-log-an-object-in-nodejs

  • Related