1

I am iterating through items in an object using the forEach method.

function scrapeRssFeedData(rssFeeds, cb){

  rssFeeds.forEach(function(rssFeed){

    rssFeed.property = 'value';
    console.log(rssFeed.property);
    console.log(rssFeed);

  });
}

The value that is logged for rssFeed.property is, correctly 'value'.

However, when the rssFeed object itself is logged, the property is not visible.

The original rssFeeds object is built using a Mongoose model.

Why can I not see the property when logging the object? What can I do to fix it?

Update: Even this code is not working:

function scrapeRssFeedData(rssFeeds, cb){

  rssFeeds[1]['property'] = 'value';

  console.log(rssFeeds[1]);

}

The fact that this example doesn't work makes me believe almost certainly it's something to do with the object having been built by Mongoose. I want to be done with Mongoose though at this point of the program and want to start adding values to this object.

Anthony
  • 13,434
  • 14
  • 60
  • 80

1 Answers1

2

As I grew to suspect, it was due to it being an instance of a Mongoose document, as opposed to just a normal Javascript object.

I was able to find the solution here:

How do you turn a Mongoose document into a plain object?

And adding .lean() into the query chain, ie:

function getRssFeeds(status, cb){
  if(status == 'on'){
    RSSFeed
    .find({})
    .populate('endpoints') 
    .lean() // <-- Needed to be added
    .exec(function (err, feeds) {
      cb(null, feeds);
    });
  }
  else if(status == 'off'){
    cb({'status' : 'functionality', 'message': 'app is turned off'}) 
  }
}
Community
  • 1
  • 1
Anthony
  • 13,434
  • 14
  • 60
  • 80