1

Upon receiving the data from the client, I would like to add a new document or update if it already exists. For all the parameters which are not defined, I would like to delete the existing keys.

The function which handles the data and is supposed to performed the saving/updating the data is the following:

router.post('/saveEvent', isAuthenticated, function(req, res){
    var eventId = req.body.eventId;
    console.log(eventId);
    var timelineId = req.body.timelineId;
    var strEventStart = req.body.eventStart;
    var strEventEnd = req.body.eventEnd;
    var mediaURI = req.body.mediaURI;
    var note = req.body.note;
    var title = req.body.title;
    var eventIdToSave;

    var event = new Event({_timeline: timelineId, title: title, description: note, mediaURI: mediaURI, eventStart: strEventStart, eventEnd: strEventEnd});

    console.log("Updated event: " + event);

    if(!(eventId == "" | eventId == undefined)){
        eventIdToSave = eventId;
    } else {
        eventIdToSave = event._id;
    }
    var upsertData = event.toObject();
    delete upsertData._id;
    console.log("event: " + upsertData);

    Event.update({_id: eventIdToSave}, upsertData, {upsert: true}, function(err){
        if(err){
            console.log(err);
        } else{
            if(eventId == "" | eventId == undefined){
                Timeline.findByIdAndUpdate(
                    timelineId,
                    {$push: {"events": eventIdToSave}},
                    {safe: true, upsert: true, new : true},
                    function(err, model) {
                        console.log(err);
                        if(!err){
                            console.log("added new event to timeline");
                            res.json({Successful: true});
                        }
                    }
                );
            } else {
                res.json({Successful: true});
            }
        }
    });
});

Below is an example console output for the received data:

Updated event: { _timeline: 55b21588c4a50f345c928e2c,
  title: 'This is perfect!',
  description: '<div><b style="line-height: 1.42857143;">This image was taken somewhere in Europe.</b><br></div>',
  mediaURI: undefined,
  eventStart: Thu Jul 30 2015 23:26:00 GMT+0200 (CEST),
  eventEnd: undefined,
  _id: 55b24aff323dc6dd6d7378ea,
  duration: false }

In this example the mediaURI and eventEnd are undefined, but the keys are not deleted; here is an example data retrieved from collection after the above operation:

events: 
   [ { _id: 55b23d265821141f65bb515d,
       mediaURI: 'https://dl.dropboxusercontent.com/1/view/xxxxx.jpg',
       description: '<div><b style="line-height: 1.42857143;">This image was taken somewhere in Europe.</b><br></div>',
       title: 'This is perfect!' } ] }

Does anyone know what is a correct way to delete all the keys of the existing document for which parameters are not received from client?

I have checked this post and don't know why the keys with undefined values are not deleted.

Community
  • 1
  • 1
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286

1 Answers1

0

maybe try this, where you have...

var upsertData = event.toObject();
delete upsertData._id;

add this beneath...

for (var theKey in upsertData) {
  if (upsertData.hasOwnProperty(theKey)) {
    if (!upsertData[theKey]) {
      delete upsertData[theKey];
    }
  }
}

or am I missing something?

WhiteHat
  • 59,912
  • 7
  • 51
  • 133