mongoose $ inc findByIdAndUpdate失败

时间:2016-09-08 09:38:49

标签: node.js mongoose auto-increment put

当我更新一个对象rappel时,我想自动增加一个attribut modif(modif是一个应该在每次更新时递增1的计数器)。但是当我使用mongoose的$ inc时它运行正常然后失败了。

   /routes/rappels.js
   /* update rappel by autoincrement modif {$inc: { modif: 1} }*/
    router.put('/:id', function(req, res, next) {
    //var data = req.body;
    //if(data.posts == undefined) data.posts = [];    
    models.rappel.findByIdAndUpdate(req.params.id, 
  {$inc: { modif: 1}},  {new: true}, function(err, c){
    if(err) res.json({error: err});

    res.json(c).send({modif: c.modif});
   });
  });

,模型是:

 var mongoose = require('../config/db');

 var RappelSchema = mongoose.Schema({
 titre: String,
 date_rappel: String,
 heure_rappel: String,
 type: String,
 modif: { type: Number, default: 0 },
 suppre: ['0', '1'],
 robot: { type: mongoose.Schema.Types.ObjectId, ref: 'Robot' }
  });

 module.exports = mongoose.model('Rappel', RappelSchema);

 it work fine when I test with Postman but after 2 updates the application failed and crushed.

这是heroku日志结果

   remote:
   remote: -----> Compressing...
   remote:        Done: 31.2M
   remote: -----> Launching...
   remote: Released v76
   remotte: https://testroomie.herokuapp.com/ deployed to Heroku
   remote:
   remote: Verifying deploy... done.

  Warning: Your console font probably doesn't support Unicode. 
 If you experience strange characters in
 the output, consider switching to a TrueType font such as Consolas!
 To https://git.heroku.com/testroomie.git
 d12e210..9a60555  master -> master

C:\OSC2016\HerokuPull28_08_16\testroomie>heroku logs
2016-09-08T07:42:42.421017+00:00 app[web.1]:at 
ServerResponse.header(/app/node_modules/express
/lib/response.js:718:10)
2016-09-08T07:42:42.421018+00:00 app[web.1]:at 
 ServerResponse.send (/app/node_modules/express/
  lib/response.js:163:12)
  2016-09-08T07:42:42.421018+00:00 app[web.1]:     
  at ServerResponse.json (/app/node_modules/express/
  lib/response.js:249:15)
  2016-09-08T07:42:42.421019+00:00 app[web.1]:     
  at Query.<anonymous>(/app/routes/rappels.js:48:21)


  2016-09-08T07:42:42.421019+00:00 app[web.1]:     
  at ServerResponse.send (/app/node_modules/express/
  lib/response.js:151:21)
  2016-09-08T07:42:42.421020+00:00 app[web.1]:     
  at /app/node_modules/kareem/index.js:177:19
  2016-09-08T07:42:42.421021+00:00 app[web.1]:   
   at /app/node_modules/kareem/index.js:109:16
  2016-09-08T07:42:42.421021+00:00 app[web.1]:    
  at _combinedTickCallback (internal/process/
  next_tick.js:67:7)
  2016-09-08T07:42:42.421022+00:00 app[web.1]:    

  at process._tickCallback (internal/process/
  next_tick.js:98:9)
  2016-09-08T07:42:42.446749+00:00 app[web.1]:
  2016-09-08T07:42:42.463235+00:00 app[web.1]: npm ERR! 
  Linux 3.13.0-93-  generic
  2016-09-08T07:42:42.463766+00:00 app[web.1]: npm ERR! 
  argv"/app/.heroku/node/bin/node" "/app/.herok
  u/node/bin/npm" "start"
  2016-09-08T07:42:42.464078+00:00 app[web.1]: npm ERR! node v5.11.1
  2016-09-08T07:42:42.465018+00:00 app[web.1]: npm ERR! npm  v3.8.6
  2016-09-08T07:42:42.465340+00:00 app[web.1]: npm ERR! code ELIFECYCLE
  2016-09-08T07:42:42.465579+00:00 app[web.1]: npm ERR! 
  PiExpress@0.0.0   start: `node ./bin/www`
  2016-09-08T07:42:42.465815+00:00 app[web.1]: npm ERR! Exit status 1
  2016-09-08T07:42:42.466082+00:00 app[web.1]: npm ERR!
  2016-09-08T07:42:42.466310+00:00 app[web.1]: npm ERR! Failed at 
  the PiExpress@0.0.0 start script 'no
  de ./bin/www'.
  2016-09-08T07:42:42.466535+00:00 app[web.1]: npm ERR! Make sure you 
  have the latest version of
  node.js and npm installed.
  2016-09-08T07:42:42.466747+00:00 app[web.1]: npm ERR! If you do, 
  this is most likely a problem with
  the PiExpress package,
  2016-09-08T07:42:42.466965+00:00 app[web.1]: npm ERR! not with npm itself.
  2016-09-08T07:42:42.467175+00:00 app[web.1]: npm ERR! Tell the author 
  that this fails on your system:
  2016-09-08T07:42:42.467403+00:00 app[web.1]: npm ERR!     node ./bin/www
  2016-09-08T07:42:42.467614+00:00 app[web.1]: npm ERR! You can 
  get information on how to open an issue for this project with:
  2016-09-08T07:42:42.467835+00:00 app[web.1]: npm ERR!npm bugs PiExpress
  2016-09-08T07:42:42.468054+00:00 app[web.1]: npm ERR! Or if that 
  isn't available, you can get their info via:
  2016-09-08T07:42:42.468274+00:00 app[web.1]:npm ERR!npm owner ls PiExpress
  2016-09-08T07:42:42.468492+00:00 app[web.1]: npm ERR! There is 
  likely additional logging output above.
  2016-09-08T07:42:42.473827+00:00 app[web.1]:
  2016-09-08T07:42:42.474143+00:00 app[web.1]: npm ERR! Please include 
  the following file with any support request:
  2016-09-08T07:42:42.474355+00:00 app[web.1]: npm ERR! /app/npm-debug.log
 2016-09-08T07:42:42.578312+00:00 heroku[web.1]:Process exited with status 1
 2016-09-08T07:42:42.613995+00:00 heroku[web.1]: State changed from 
  up to  crashed
  2016-09-08T07:42:42.615056+00:00 heroku[web.1]: State changed from 
   crashed to starting
  2016-09-08T07:42:45.070840+00:00 heroku[web.1]: Starting process 
  with command `npm start`
  2016-09-08T07:42:47.367719+00:00 app[web.1]:
  2016-09-08T07:42:47.367733+00:00 app[web.1]: > PiExpress@0.0.0 start /app
  2016-09-08T07:42:47.367734+00:00 app[web.1]: > node ./bin/www
  2016-09-08T07:42:47.367735+00:00 app[web.1]:
  2016-09-08T07:42:48.034029+00:00 app[web.1]: mongo connected
  2016-09-08T07:42:48.641807+00:00 heroku[web.1]: State changed 
  from starting to up
  2016-09-08T07:43:41.999170+00:00 heroku[router]: at=info 
  method=POST    path="/authenticate/" host=test
  roomie.herokuapp.com 
  request_id=2c5f72c8-e3ac-4797-88b4-2c84301eac87 
  fwd="41.224.13.74"dyno=web.1 connect=1ms service=84ms status=200bytes=1967
  2016-09-08T07:43:41.994683+00:00 app[web.1]: POST /authenticate/ 
 200 70.400   ms - 1559
 2016-09-08T07:44:14.756752+00:00 heroku[router]: 
 at=info method=PUT     path="/rappels/57d03a51f67f121100af8cf3"
 host=testroomie.herokuapp.com 
 request_id=c8db23e5-daf1-46d8-a984-f2bba76db5d9 
 fwd="41.224.13.74"   dyno=web.1 connect=0ms service=24ms 
 status=200 bytes=608
 2016-09-08T07:44:14.772687+00:00 app[web.1]: _http_outgoing.js:346
 2016-09-08T07:44:14.772723+00:00 app[web.1]:throw new Error
 ('Can\'t set  headers after they are sent.');
 2016-09-08T07:44:14.772732+00:00 app[web.1]:     ^
  2016-09-08T07:44:14.772740+00:00 app[web.1]:
  2016-09-08T07:44:14.772749+00:00 app[web.1]: 
  Error: Can't set headers after they are sent.
  2016-09-08T07:44:14.772751+00:00 app[web.1]:
  at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
  2016-09-08T07:44:14.772751+00:00 app[web.1]:     
  at ServerResponse.header (/app/node_modules/expres/lib/response.js:718:10)
  2016-09-08T07:44:14.772752+00:00 app[web.1]:     
  at ServerResponse.send(/app/node_modules/express/lib/response.js:163:12)
  2016-09-08T07:44:14.772752+00:00 app[web.1]:     
   at ServerResponse.json (/app/node_modules/express/lib/response.js:249:15)
   2016-09-08T07:44:14.772753+00:00 app[web.1]:     
   at ServerResponse.send (/app/node_modules/express/lib/response.js:151:21)
   2016-09-08T07:44:14.772754+00:00 app[web.1]:     
   at Query.<anonymous> (/app/routes/rappels.js:48:21)

   2016-09-08T07:44:14.772754+00:00 app[web.1]:     
   at /app/node_modules/kareem/index.js:177:19
   2016-09-08T07:44:14.772755+00:00 app[web.1]:     
   at   /app/node_modules/kareem/index.js:109:16
   2016-09-08T07:44:14.772755+00:00 app[web.1]:     
   at _combinedTickCallback (internal/process/next_tick.js:67:7)
   2016-09-08T07:44:14.772764+00:00 app[web.1]:     
    at process._tickCallback (internal/process/next_tick.js:98:9)
     2016-09-08T07:44:14.785178+00:00 app[web.1]:
    2016-09-08T07:44:14.794981+00:00 app[web.1]: npm ERR! 
     Linux 3.13.0-91- generic
     2016-09-08T07:44:14.795427+00:00 app[web.1]: npm ERR! 
     argv   "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
    2016-09-08T07:44:14.795700+00:00 app[web.1]: npm ERR! node v5.11.1
    2016-09-08T07:44:14.796770+00:00 app[web.1]: npm ERR! code ELIFECYCLE
    2016-09-08T07:44:14.796544+00:00 app[web.1]: npm ERR! npm  v3.8.6
    2016-09-08T07:44:14.796979+00:00 app[web.1]: npm ERR! 
    PiExpress@0.0.0 start: `node ./bin/www`
    2016-09-08T07:44:14.797144+00:00 app[web.1]: npm ERR! Exit status 1
    2016-09-08T07:44:14.797351+00:00 app[web.1]: npm ERR!
    2016-09-08T07:44:14.797529+00:00 app[web.1]: npm ERR! 
    Failed at the PiExpress@0.0.0 start script 'node ./bin/www'.
    2016-09-08T07:44:14.797905+00:00 app[web.1]: npm ERR! If you do, 
    this is most likely a problem with the PiExpress package,
    2016-09-08T07:44:14.797720+00:00 app[web.1]: npm ERR! Make sure 
    you have the latest version of node.js and npm installed.
    2016-09-08T07:44:14.798078+00:00 app[web.1]: npm ERR! 
    not with npm itself.
    2016-09-08T07:44:14.798255+00:00 app[web.1]: npm ERR! 
    Tell the author  that this fails on your system:
    2016-09-08T07:44:14.798477+00:00 app[web.1]: npm ERR!     node ./bin/www
    2016-09-08T07:44:14.798671+00:00 app[web.1]: npm ERR! 
    You can get information on how to open an issue for this project with:
    2016-09-08T07:44:14.798849+00:00 app[web.1]: npm ERR!     
   npm bugs PiExpress
   2016-09-08T07:44:14.799041+00:00 app[web.1]: npm ERR! 
   Or if that isn't available, you can get their info via:
   2016-09-08T07:44:14.799211+00:00 app[web.1]: npm ERR!     
  npm owner ls PiExpress
  2016-09-08T07:44:14.799394+00:00 app[web.1]: npm ERR! There is 
  likely   additional logging output above.
  2016-09-08T07:44:14.805265+00:00 app[web.1]:
  2016-09-08T07:44:14.805483+00:00 app[web.1]: npm ERR! Please 
  include the following file with any support request:
  2016-09-08T07:44:14.805611+00:00 app[web.1]: npm ERR!     
  /app/npm-debug.log
  2016-09-08T07:44:14.932783+00:00 heroku[web.1]: State changed 
  from up to crashed
  2016-09-08T07:44:14.933664+00:00 heroku[web.1]: Process exited 
  with status 1
  2016-09-08T07:44:19.222339+00:00 heroku[router]: at=error 
  code=H10 desc="App crashed"
  method=PUT    path="/rappels/57d03a51f67f121100af8cf3" 
  host=testroomie.herokuapp.com request_id=9ffc6580-6d50-4c0d-b4
  06-2dfd96860df2 fwd="41.224.13.74" dyno= connect= service= 
  status=503 bytes=
  2016-09-08T07:45:18.507804+00:00 heroku[router]: at=error 
  code=H10 desc="App crashed" method=GET 
  path="/rappels" host=testroomie.herokuapp.com 
  request_id=9305aa1d-af7f-4db0-972a-e053d5b3dda8 
  fwd="41.224.13.74" dyno= connect= service= status=503 bytes=


  2016-09-08T07:46:19.868822+00:00 heroku[slug-compiler]: 
  Slug compilation started
  2016-09-08T07:46:19.868831+00:00 heroku[slug-compiler]: 
  Slug compilation finished
  2016-09-08T07:46:19.889451+00:00 heroku[web.1]: 
  State changed from crashed to starting
  2016-09-08T07:46:22.338101+00:00 heroku[web.1]: 
  Starting process with command `npm start`
  2016-09-08T07:46:24.132828+00:00 app[web.1]:
  2016-09-08T07:46:24.132841+00:00 app[web.1]: > PiExpress@0.0.0 start /app
  2016-09-08T07:46:24.132842+00:00 app[web.1]: > node ./bin/www
  2016-09-08T07:46:24.132842+00:00 app[web.1]:
 2016-09-08T07:46:24.845254+00:00 app[web.1]: mongo connected
 2016-09-08T07:46:25.139671+00:00 heroku[web.1]: State changed 
 from starting to up
 2016-09-08T07:46:27.070536+00:00 heroku[router]: at=info 
 method=POST  path="/authenticate/" 
 host=testroomie.herokuapp.com 
 request_id=1a55a638-2e8f-4b22-bc05-3108962d855c      
 fwd="41.224.13.74" dyno=web.1 connect=1ms service=54ms 
 status=200 bytes=1967
2016-09-08T07:46:27.071806+00:00 app[web.1]: 
POST /authenticate/ 200 44.765   ms - 1559

1 个答案:

答案 0 :(得分:0)

在应用程序的heroku日志中,此行
错误:发送标头后无法设置。

您正在发送响应,但没有返回语句。因此,如果由于某种原因发生错误,则属于if检查(您的代码):

if(err) res.json({error: err});

但它不会退出流程,下一部分也会在返回响应的地方执行。

res.json(c).send({modif: c.modif});

所以发送响应的正确方法是

router.put('/:id', function(req, res, next) {
        models.rappel.findByIdAndUpdate(req.params.id, 
      {$inc: { modif: 1}},  {new: true}, function(err, c){
        if(err) return res.json({error: err});

        return res.json(c).send({modif: c.modif});
       });
      });