Enrique Alcazar
Enrique Alcazar

Reputation: 381

Can't add attribute to object JS

I'm recovering an object from mongoose, I can see and access all it's attributes, but I can't create new attributes for that object.

What I tried

transfer.test= 'test';

Order of calls:

  console.log(transfer);
  transfer.test = 'test';
  console.log(transfer);
  console.log(transfer.test);

Output:

{ _id: '0000000000000303',
  Ammount: '1',
  State: 'SUCCESS',
  TimeStampStarted: '1512043753',
  LastUpdate: '1512043763',
  __v: 0 }
{ _id: '0000000000000303',
  Ammount: '1',
  State: 'SUCCESS',
  TimeStampStarted: '1512043753',
  LastUpdate: '1512043763',
  __v: 0 }
test

Why isn't it there? why am I not getting some error, warning etc?

PD I checked the type of transfer, it's an object

Upvotes: 1

Views: 487

Answers (1)

David Vicente
David Vicente

Reputation: 3111

What you see when console.log a Mongoose object is not all its info, just a representation. Mongoose objects have a lot of variables that Mongoose needs to manage it. Sometimes, this represantion is not updated. Although it is storing the information correctly in the object, it is not printing it well. If you don't need Mongoose characteristics in this object anymore, you can do

transfer = transfer.toObject();

and it will be a JavaScript object from now on, and it should show everything updated.

Upvotes: 2

Related Questions