title: Mongoose Schema Error: "Cast to string failed for value" when pushing object to empty array
body: <p>I have a strange problem and cannot figure out what the problem is. The Error-message doesn't help.</p>
  <p>I'm sending an &quot;alarm&quot; to the server and want to save this alarm to my &quot;device&quot; which already exist in the database.</p>
  <p>The alarm object I send to the server looks like this:</p>
  <pre><code>{
    actionTaken: &quot;none&quot;, 
    dateTime: &quot;20152111191512&quot;, 
    difference: 4.88, 
    timestamp: 1448128894781
  }
  </code></pre>
  <p>The Schema for the device is as follows:</p>
  <pre><code>var deviceSchema = new Schema({
     deviceId: {
          type : String,
          index : {
              unique : true,
              dropDups : true
          }
      },
      alarms : [ {
          timestamp : Number,
          dateTime : String, //yyyymmddhhss
          difference : Number,
          actionTaken : String, //&quot;send sms&quot;
      } ]
  });
  </code></pre>
  <p>I load the device from the database (<code>deviceId</code> is set):</p>
  <pre><code>Thermometer.findOne({
          deviceId : deviceId
  }, function(error, device){ 
     //error handling
     var now = (new Date().getTime());
     var nowDateTime = (new Date()).toISOString().slice(0, 19).replace(/[-T\s:]/g, &quot;&quot;);
     var newAlarm = {
         timestamp : now,
         dateTime : nowDateTime, // yyyymmddhhmmss
         difference : diff,
         actionTaken : &quot;none&quot;
     };
     device.alarms.push(newAlarm);  //EXCEPTION !
  
     //       device.save //doesn't get called
  });
  </code></pre>
  <p>As you can see in the comment, I get an Exception/Error when I want to push the &quot;newAlarm&quot;-object to the alarms-array of my device.</p>
  <p>The Error says:</p>
  <blockquote>
  <p>Cast to string failed for value <code>[object Object]</code> at path <code>alarms</code></p>
  </blockquote>
  <p>Error-Object:</p>
  <pre><code>   kind: &quot;string&quot;,
     message: &quot;Cast to string failed for value &quot;[object Object]&quot; at path &quot;alarms&quot;&quot;,
     name: &quot;CaseError&quot;,
     path: &quot;alarms&quot;,
     stack: undefined,
     value: {actionTaken: &quot;none&quot;, dateTime: &quot;20152111191512&quot;, difference: 4.88, timestamp: 1448128894781}
  </code></pre>
  <p>Do you have an idea?</p>
  <p>For me it doesn't make any sense. The array and its content (object) is specified in the Schema. Why is there a string cast error with the whole object as value?</p>
  <p>What I use:</p>
  <pre><code>&quot;express&quot;: &quot;3.2.6&quot;,
  &quot;express-session&quot;:&quot;1.7.6&quot;,
  &quot;hjs&quot;: &quot;*&quot;,
  &quot;mongoose&quot;: &quot;4.0.5&quot;,
  &quot;nodemailer&quot;: &quot;1.4.0&quot;
  </code></pre>
  <hr />
  <p><strong>EDIT:</strong> I don't want to use nested Schemas. It is also possible to do it with arrays. I do it with arrays in some other Schemas.</p>
  <p><strong>EDIT 2</strong>:
  I added an property <code>lastAlarm</code> and do</p>
  <pre><code>device.lastAlarm = alarm;
  </code></pre>
  <p>but after that, thermometer.lastAlarm is still undefined... but alarm is an object. So is it possible that the device object is locked some how?</p>