module.exports = serialize;

function serialize(model, mapping) {
  if (mapping.properties) {
    var serializedForm = {};

    for (var field in mapping.properties) {
      var val = serialize(model[field], mapping.properties[field]);
      if (val !== undefined) {
        serializedForm[field] = val;
      }
    }

    return serializedForm;

  } else if (typeof value === 'object' && value !== null) {
    var name = value.constructor.name;
    if (name === 'ObjectID') {
      return value.toString();
    } else if (name === 'Date') {
      return new Date(value).toJSON();
    }
  } else {
    return model;
  }
}
@@ -157,24 +157,45 @@
 operties) %7B%0A
+      if (model) %7B%0A  
       var va
@@ -250,16 +250,18 @@
 ield%5D);%0A
+  
       if
@@ -291,16 +291,18 @@
         
+  
 serializ
@@ -322,16 +322,26 @@
  = val;%0A
+        %7D%0A
       %7D%0A

make serialize more robust for deep models with empty parts