0

I need to write an array of places to a JSON file, I take the places from the MongoDB. The code below work

'use strict';
const jsonfile = require('jsonfile');
const debug = require('debug')('myproject:server');
const Place = require('../models/place.js');
const path = './public/places.json';


Place.find({
    reviewed: true
  }, (err, places) => {
    if (err) {
      debug(err);
    } else {
      jsonfile.writeFile(path, places, (err) => {
        if (err) {
          debug(err);
        }
      });
    }
  });

This is what a single object in the JSON file looks like

{ _id: 58b8d11cbaa41f288236d5fa,
  __v: 0,
  mainImg: 'placeImg-1490464803517.jpg',
  reviewed: true,
  date: 2017-03-03T02:12:44.313Z,
  description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum illo odit animi architecto.',
  coord: { lng: 2.166948616504669, lat: 41.382971076851476 },
  type: 'museum',
  title: 'Lorem' }

Since I have a lot of objects in the array places it makes sense to remove the properties that are not used on the client side, such as __v and reviewed

I tried doing following before writing array to the file

let shorterPlaces = [];
places.forEach((el, i) => {
  delete el['__v'];
  delete el['reviewed'];
  shorterPlaces.push(el);
});

and then writing shorterPlaces to a file, but the properties remained.

When I tried logging out the object keys inside the for each loop with console.log(Object.keys(el)); I got [ '$__', 'isNew', 'errors', '_doc' ] which does not make any sense to me. Is there something that I am missing here or unaware of?

5
  • The code that deletes properties looks fine to me, can you show how you used this modification? Commented May 21, 2017 at 19:28
  • You can use projection to remove the fields from the response. Something like Place.find({ reviewed: true }, '-__v -reviewed', (err, places) .... Commented May 22, 2017 at 0:39
  • @MariaInesParnisari the properties are not removed with delete Commented May 22, 2017 at 11:11
  • @Veeram can you please post your comment as an answer so I can mark it as correct? Commented May 22, 2017 at 11:11
  • Possible duplicate of Mongoose, find, return specific properties Commented May 22, 2017 at 11:47

1 Answer 1

2

Based on the file you're requireing it looks like you're using Mongoose. Mongoose collection's find method returns Mongoose documents, which are objects with their own methods and getters/setters, rather than just the plain data in the database.

If you want just the data in a regular JS object, you need to use lean:

Place.find({ reviewed: true })
  .lean()
  .exec((err, places) => {
     // Here, places will be an array of plain objects that you can
     // manipulate as normal
   })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.