节点驱动程序MongoDb - 未定义handleCallbaak而不是catch()触发

时间:2017-11-20 03:11:36

标签: node.js mongodb node-mongodb-native

使用Node 8. *和MongoDb驱动程序mongodb@2.2.31

我正在测试我的代码库,看看我的MongoDB崩溃后它的行为方式。我测试的方法是关闭MongoDB守护进程。

catch()语句未执行。相反,当执行 ReferenceError: handleCallbaak is not defined时,我会收到mongodb驱动程序错误mongo.tempUsers.add(body, token, password)

这是为什么?不应该catch()执行吗?

TempUsers.prototype.add = function(body, token) {
  const user = { "email" : body.email };

  return this.collection.updateOne(user,
    { $set: {
      "token": token,
      "createdAt": new Date(),
      }
    },
    {
      writeConcern: true,
      maxTimeMS: QUERY_TIME,
      upsert: true
    }
  );
const confirmUser = (body, password) => {
  const token = uuidv4();
  const result = mongo.tempUsers.add(body, token, password);
  result.then(() => sendConfirmationEmail(body, token)).catch(e => console.log(e));
};

更新:如何创建自己的collections对象:

   var collections = {
     tempUsers: false
   };

function connect(dbURI) {
  return MongoClient
    .connect(dbURI)
    .then(function(db) {
      console.log(colors
        .bold('MongoDB default connection open to ' + dbURI));
      initDbListeners(db);
      initCollections(db);
      return db;
    })
    .catch(function(err) {
      console.log(colors
        .red(err));
    });
}

function initCollections(db) {
  collections.tempUsers = new TempUsers(db);
}


module.exports.collections = collections;
/home/one/github/dolphin/node_modules/mongodb/lib/collection.js:1057
    if(err) return handleCallbaak(callback, err, null);
            ^

ReferenceError: handleCallbaak is not defined
    at Object.c (/home/one/github/dolphin/node_modules/mongodb/lib/collection.js:1057:13)
    at Store.flush (/home/one/github/dolphin/node_modules/mongodb/lib/topology_base.js:68:30)
    at Server.reconnectFailedHandler (/home/one/github/dolphin/node_modules/mongodb/lib/server.js:290:18)
    at emitOne (events.js:115:13)
    at Server.emit (events.js:210:7)
    at Pool.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/topologies/server.js:324:14)
    at emitOne (events.js:115:13)
    at Pool.emit (events.js:210:7)
    at Connection.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/connection/pool.js:317:16)
    at emitTwo (events.js:125:13)
    at Connection.emit (events.js:213:7)
    at Socket.<anonymous> (/home/one/github/dolphin/node_modules/mongodb-core/lib/connection/connection.js:187:49)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)

1 个答案:

答案 0 :(得分:1)

您系统上的mongodb node_modules/mongodb/lib/collection.js文件与released file不同。

从项目中删除/home/one/github/dolphin/node_modules/mongodb目录,然后再次运行npm install