node.js - mongodb incremental numbering - 500 internal server error -


i trying number id's of documents node , mongodb. getting 500 internal server error: 'cannot read property 'seq' of null.

// submit ticket - returning 500 error - not updating id

router.post('/ticketform', function(req, res){     var db = req.db;  function getnextsequence(name, callback){     db.counters.findandmodify(         {             query:{ _id:name},             update:{$inc:{seq:1} },             new:true         }, callback     ); }      getnextsequence('ticket', function(err, obj) {         if (err) console.error(err);         db.users.insert(         {             '_id': obj.seq,             'firstname':req.body.firstname,             'lastname':req.body.lastname,             'email':req.body.email,             'phone':req.body.phone,             'issue':req.body.issue         }, function(err,docs) {             if (err) console.error(err);             console.log(docs);             res.end();         });     }); }); 

you appear using node native driver in syntax bit different mongodb shell. .findandmodify() function in particular works quite differently.

as complete working example work application there code, along usage of async module make logic little cleaner:

var async = require("async"),     mongodb = require("mongodb"),     mongoclient = mongodb.mongoclient;   mongoclient.connect('mongodb://localhost/test',function(err,db) {      async.waterfall(         [             function(callback) {                 db.collection("counters").findandmodify(                     { "_id": "ticket" },                     [],                     { "$inc": { "seq": 1 } },                     { "upsert": true, "new": true },                     function(err,doc) {                         callback( err, doc.seq );                     }                 );             },             function(seq, callback) {                 var doc = { "_id": seq };                 db.collection("users").insert(                     { "_id": seq },                     function(err, doc) {                       callback( err, doc );                     }                 );             }         ], function( err, result ) {              console.log( result );         }     ); }); 

the parameters on function "query", "sort" array , required if blank, "update" document, "options" in want updated document returned "upsert" create new document no matching document exists, , callback.

the async waterfall allows "pass through" results without embedding functionality within callback of method use sequence. don't need way, code looks little cleaner , direct in it's purpose.

alternately there options such mongoose if want use has helper function in .findoneandupdate(). might find connection management little easier library, you.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -