title: Cast to Number failed for value "NaN" at path error solved by server self
body: <p>I'm a very beginner of Nodejs, not sure I can ask such an easy thing here.
  When I built a node server to connect to mongodb, got this error:</p>
  
  <blockquote>
    <p>Cast to Number failed for value "NaN" at path "count"</p>
  </blockquote>
  
  <p>But, the error is gone in few minutes and the server is running again somehow as well as the count schema was inserted.</p>
  
  <p>I can't find something what made it run again.
  I just assumed for the reason that my node server couldn't get the value because the "count" schema disappeared. </p>
  
  <p>But could you help me to think in better ways.</p>
  
  <p>Here is the error what I had before.</p>
  
  <ul>
  <li>First, there wasn't "count" schema when I checked datas collection in mongodb. </li>
  </ul>
  
  <pre><code>&gt; db.datas.find()
  { "_id" : ObjectId("5cb39eca6e5e3971fa934a2d"), "name" : "myData", "__v" : 0 }
  </code></pre>
  
  <ul>
  <li>Second, node server console showed me the error below:</li>
  </ul>
  
  <pre><code>Data ERROR: save:  { ValidationError: data validation failed: count: Cast to Number failed for value "NaN" at path "count"
      at ValidationError.inspect (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/error/validation.js:59:24)
      at formatValue (util.js:430:38)
      at inspect (util.js:324:10)
      at format (util.js:253:18)
      at Console.log (console.js:130:21)
      at /home/gkh/Desktop/Dev/myapp/app.js:46:30
      at /home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/model.js:4675:16
      at /home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/utils.js:255:11
      at $__save.error (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/model.js:471:16)
      at /home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:246:48
      at next (/home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:167:27)
      at next (/home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:169:9)
      at Kareem.execPost (/home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:217:3)
      at _handleWrapError (/home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:245:21)
      at /home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:272:14
      at _next (/home/gkh/Desktop/Dev/myapp/node_modules/kareem/index.js:94:14)
    errors: 
     { count: 
        { CastError: Cast to Number failed for value "NaN" at path "count"
      at new CastError (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/error/cast.js:29:11)
      at model.$set (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/document.js:1073:7)
      at model.set [as count] (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/helpers/document/compile.js:140:26)
      at /home/gkh/Desktop/Dev/myapp/app.js:44:15
      at /home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/model.js:4675:16
      at /home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/query.js:4184:12
      at process.nextTick (/home/gkh/Desktop/Dev/myapp/node_modules/mongoose/lib/query.js:2741:28)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
          message: 'Cast to Number failed for value "NaN" at path "count"',
          name: 'CastError',
          stringValue: '"NaN"',
          kind: 'Number',
          value: NaN,
          path: 'count',
          reason: [Object] } },
    _message: 'data validation failed',
    name: 'ValidationError' }
  </code></pre>
  
  <ul>
  <li>Here is part of an app.js file:</li>
  </ul>
  
  <pre><code>mongoose.connect("mongodb://localhost:27017/user-node");
  var mongod = mongoose.connection;
  mongod.once("open", function(){
    console.log("DB connected!");
  });
  mongod.on("err", function(err){
    console.log("DB err: ", err);
  });
  
  var dataSchema = mongoose.Schema({
    name:String,
    count:Number
  });
  
  var Data = mongoose.model('data', dataSchema);
  Data.findOne({name:"myData"}, function(err, data){
    if(err) return console.log("Data ERROR: ", err);
    if(!data){
      Data.create({name:"myData",count:0}, function(err, data){
        if(err) return console.log("Data ERROR", err);
        console.log("Counter initialized: ", data);
      });
    }
  });
  
  app.set("view engine", 'ejs');
  app.use(express.static(path.join(__dirname + '/public')));
  
  // callback function list
  // render to firstPage.ejs page with params or non-params
  app.get('/', function(req, res){
    Data.findOne({name:"myData"}, function(err, data){
      if(err) return console.log("Data ERROR : /path: ", err);
      data.count++;
      data.save(function(err){
        if(err) return console.log("Data ERROR: save: ", err);
        res.render('firstPage', data);
      });
    });
  });
  // set count to 0
  app.get('/reset', function(req, res){
    setCounter(res, 0);
  });
  // check arg in req.query.count
  app.get('/set/count', function(req, res){
    if(req.query.count) setCounter(res, req.query.count);
    else getCounter(res);
  });
  // placeholder : num
  // any arg can place in num param
  app.get('/set/:num', function(req, res){
    if(req.params.num) setCounter(res, req.params.num);
    else getCounter(res);
  });
  
  function setCounter(res, num){
    console.log("...setCounter...");
    Data.findOne({name:"myData"}, function(err, data){
      if(err) return console.log("Data ERROR: ", err);
      data.count=num;
      data.save(function(err){
        if(err) return console.log("Data ERROR: ", err);
        res.render('firstPage', data);
      });
    });
  }
  
  function getCounter(res){
    console.log("...getCounter...");
    Data.findOne({name:"myData"}, function(err, data){
      if(err) return console.log("Data ERROR", err);
      res.render('firstPage', data);
    });
  }
  </code></pre>
  
  <p>If I'm wrong or my code is wrong, please advice for me.
  Thank you in advance!</p>