A
A
Artem Komarov2014-06-25 15:42:14
JavaScript
Artem Komarov, 2014-06-25 15:42:14

How to overcome callback hell in nodejs+mongodb? Can you help me understand where to dig in search of errors?

The bottom line is - I'm trying to write a simple database patch with the following code:

exports.checkVersion = function(serverVersion){
  MongoClient.connect(mn.path(), function(error, database){
    if (!error) {
      database.collection('params').findOne({"name": "version"}, function(err,version){
        var i = 0;
        if(!err && version && version.value) {
          if(serverVersion > version.value) {
            for (i = version.value; i <= serverVersion; ++i){
              var patch = require('./patches/'+i+'_patch.js');
              patch.execPatch(database, i, function(err){
                console.log(err);
              });
            }
          }
        }
        else {
          for (i = 0; i <= serverVersion; i++){
            var patch = require('./patches/'+i+'_patch.js');
            patch.execPatch(database, i, function(err){
              console.log(err);
            });
          }
        }
      })
    }
    else { console.dir(error); }
  });
};

Sample code for the patch itself:
exports.execPatch = function(database, serverVersion, callback){
  database.collection('params').insert({"name":"version","value":serverVersion},function(err){callback(err);});
  ...ну и ещё пара идентичных операций...
}

So. The first patch is executed completely and without errors, and the second is not executed at all ... Well, this is what it shows me:
/home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/connection/base.js:245
        throw message;      
              ^
TypeError: undefined is not a function
    at /home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/db.js:1000:14
    at /home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/db.js:445:7
    at /home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/cursor.js:163:16
    at commandHandler (/home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/cursor.js:709:16)
    at /home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/db.js:1846:9
    at Server.Base._callHandler (/home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/connection/base.js:445:41)
    at /home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/connection/server.js:478:18
    at MongoReply.parseBody (/home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/home/m0sk1t/WebStormProjects/project/node_modules/mongodb/lib/mongodb/connection/server.js:436:20)
    at EventEmitter.emit (events.js:95:17)

None of my files are in the listing, tell me where to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey P, 2016-02-19
@ruddy22

<div class="content" id="1-content" style="display:block;">

A
Artem Komarov, 2014-06-25
@m0sk1t

Beat the idiot and send him on vacation ... I figured it out =) It was in another section of the code, I
changed it to

database.collection('allMarks',function(err, allMarks){allMarks.update(...);}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question