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?
via AntK
No comments:
Post a Comment