NotBad
NotBad

Reputation: 47

NodeJS - object of an object is undefined?

I'm trying to access a JSON object but I'm getting errors while accessing the object id. Could you please go through the code and let me know the mistake I'm making.

When I try to console.log(obj.id); it says undefined

Here is the code:

  var orderidfirstuser = fs.readFileSync(`orderids/${orderid}.json`,'utf-8');
  var obj = JSON.parse(orderidfirstuser);
  obj.id = (obj.id);
  console.log(obj.id);
  obj.delivered = (obj.delivered);
  obj.title = (obj.title);
  obj.is_replacement = (obj.is_replacement);
  obj.paid_at = (obj.paid_at);

Upvotes: 1

Views: 244

Answers (1)

NotBad
NotBad

Reputation: 47

It turns out that I was saving the unparsed object. That's why when I tried to read it caused this issue.

Upvotes: 1

Related Questions