title: Get and store all changed values in data
body: <p>I am trying to create an "activity log" type thing on my server.  I am using Angular and Mongoose on a node server.  What I am trying to achieve is compare the request body with data stored in the DB by doing a deep diff.</p>
  
  <p>So, whenever an API is hit, I am running this function:</p>
  
  <pre><code>createActivityInformation(currentObject, updatedObject) {
  
          let information = [];
  
          function getChangedItem(currentItem, updatedItem, key, prop){
  
              if(!currentItem || !updatedItem) return
  
              // IF OBJECTS
              if(typeof(updatedItem) === "object" &amp;&amp; !Array.isArray(updatedItem)){           
                  for(let prop in updatedItem){
                      getChangedItem(currentItem[prop], updatedItem[prop], key, prop) 
                  }
              }
              else if(Array.isArray(updatedItem)){
              }
              else{
                  if(key != 'updatedAt' &amp;&amp; (currentItem &amp;&amp; updatedItem) &amp;&amp; (currentItem.toString() !== updatedItem.toString())){
                      // CHECK FOR DATE - THIS IS ADDED BECAUSE DATES ARE COMPARED DIFFERENT THAN TO STRINGS
                      if(currentItem instanceof Date &amp;&amp; !isNaN(currentItem)){
                          if(new Date(currentItem).getTime() != new Date(updatedItem).getTime()){
                              information.push({
                                  'changeFrom': { key: key, value: currentItem, prop: prop},
                                  'changeTo': { key: key, value: updatedItem, prop: prop}
                              })
                          }
                      } else {
                          information.push({
                              'changeFrom': { key: key, value: currentItem, prop: prop},
                              'changeTo': { key: key, value: updatedItem, prop: prop}
                          })
                      }                     
                  }
              }
          }
  
          for(let key in updatedObject){
              getChangedItem(currentObject[key], updatedObject[key], key)            
          }
  
          return information;
  
      }
  </code></pre>
  
  <p>Essentially what I am trying to do is store the original data, and the changed data.  But I cannot seem to get it to work perfectly.  The method is supposed to do a deep diff to compare everything, but also exclude some fields, like for instance <code>updatedAt</code>, or <code>_id</code>.</p>
  
  <p>I took a look at the npm package <code>deep-diff</code>, but that did not work.  That even compares the mongoose wrappers, but also compares for instance strings with booleans which then gets returned.  But I also want to save the current data as well as the updated data.</p>
  
  <p>Is there maybe something else I can look at that might solve my problem?</p>
  
  <p><strong>EDIT</strong>
  Normal objects, strings, and numbers are fine.  But nested objects, arrays and booleans seem to be giving me a hard time.</p>
  
  <p>here is a fiddle:
  <a href="https://jsfiddle.net/xgpcm0h3/" rel="nofollow noreferrer">https://jsfiddle.net/xgpcm0h3/</a></p>