title: Unexpected End of Input Error
body: <p>I have this nodejs app and one of the calls I've made is an upsert call that upserts the request body to mongodb. If the request body exists in mongo, update it, else insert it. The insert portion works correctly, however, I am having trouble with the upsert portion. My request body is being generated by a C# app that I've written. </p>
  
  <p>Ok, so when I update, I need to use the $addToSet and $each commands because I am adding elements into an array in the mongo document. </p>
  
  <p>Here is an example request body (this is a POST), this is what's sent to my nodejs app:</p>
  
  <pre><code>{
      "$addToSet": {
          "Data": {
              "$each": [
                  {"ID":"10","RandNum":"45"},
                  {"ID":"11","RandNum":"1"},
                  {"ID":"12","RandNum":"3"}
              ]
          }
       }
  }
  </code></pre>
  
  <p>The document is formatted like this in mongo:</p>
  
  <pre><code>{
      "Timestamp": "timestamp",
      "Id": "12345",
      "Header": {
          "Name": "Name",
          "Query": "SELECT * FROM test"
      },
      "Data": [
          {
              "ID": "1",
              "RandNum": "34"
          }, {
              "ID": "4",
              "RandNum": "23"
          }
      ]
  }
  </code></pre>
  
  <p>I am only interested in inserting into the "Data" array so I am using the $addToSet and $each operators. </p>
  
  <p>This is my upsert call on the nodejs app, the actual function has a lot of code so I'll just put the upsert parts:</p>
  
  <pre><code>var db = require('../db.js');
  var ind = require('./index.js');
  var mongo = require('mongodb').MongoClient;
  
  exports.upsert = function(req, res) {
      var token = req.params.token;
      var app = req.params.applicationId;
      var dataObject = {
          "Status": 1,
          "DataObject": {},
          "UserFriendlyErrorMessage": "",
          "DeveloperErrorMessage": ""
      };
  
      // set response header
      res.writeHead(200, {"Content-Type": "application/json"});
      res.writeHead(200, {"Content-Length": Buffer.byteLength(req.body)});
  
      var upsertDatabase = mongo.connect(ind.mongoLoc + databaseName, function(err, newDb) {
          if(err)
              throw err;
          var upsertCollection = newDb.collection(collectionName);
          if(newDb) {
              if(req.query.query) {
                  // UPSERT
                  try {
                      var json = JSON.parse(req.query.query);
                      upsertCollection.update(json, req.body, {upsert: true, multi: false}, function(err, records) {
                          if(err)
                              throw err;
                          console.log("Data updated");
                      });
                  } catch(e) {
                      dataObject["DataObject"] = null;
                      dataObject["UserFriendlyErrorMessage"] = "Could not update";
                      dataObject["DeveloperErrorMessage"] = e;
                      res.end(JSON.stringify(dataObject, 0, 4));
                      console.log(e);
                  }
              } else {
                   // INSERT
                   upsertCollection.insert(req.body, function(err, records) {
                      if(err)
                          throw err;
                      console.log("Data inserted");
                   });
               }
               dataObject["Status"] = 0;
               dataObject["DataObject"] = null;
               res.end(JSON.stringify(dataObject, 0, 4));
           } else {
              // could not connect to database
              dataObject["DataObject"] = null;
              dataObject["UserFriendlyErrorMessage"] = "Could not connect to database";
              dataObject["DeveloperErrorMessage"] = "ERROR: Could not connect to database";
              res.end(JSON.stringify(dataObject, 0, 4));
          }
      });
  };
  </code></pre>
  
  <p>In this function, I am testing to see if the document exists and then upserting it if it does. That's the part I'm focusing on. The dataObject is what I'm returning to the user to show errors/successes. </p>
  
  <p>When I post the data to this call, I get this error (I'm viewing the POST call in Fiddler):</p>
  
  <pre><code>HTTP/1.1 504 Fiddler - Receive Failure
  Date: Fri, 28 Feb 2014 15:30:39 GMT
  Content-Type: text/html; charset=UTF-8
  Connection: close
  Cache-Control: no-cache, must-revalidate
  Timestamp: 10:30:39.535
  
  [Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned 0 bytes
  </code></pre>
  
  <p>Content-Type is supposed to be application/json</p>