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

Node Driver MongoDb - handleCallbaak is not defined instead of catch() firing

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

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

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

这是为什么? 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)

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

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