node.js - 在 mongodb nodejs 应用程序上使用客户端 session 时出现 `Use of expired sessions is not permitted` 错误

标签 node.js mongodb

我的 mongodb 是 3.6,我的 nodejs 应用程序使用的是 mongo 客户端 3.0。下面是使用mongo client session保证因果关系的源码:

const session = client.startSession();
  const col = client.db('test').collection('test');
  col
    .insert({a: true}, {w: 0, j: false, session})
    .then(() => {
      return col.count({session});
    })
    .then(ret => {
      console.log(ret);
    }).catch(err => console.error(err));
  session.endSession();
}

运行此应用程序时出现以下错误。它无需为 insertcount 命令指定 session 即可工作。我想知道在这种情况下如何使用 session 。

    { MongoError: Use of expired sessions is not permitted
    at executeOperation (/Users/joey/dev/mongodb/demo/node_modules/mongodb/lib/utils.js:390:13)
    at Collection.count (/Users/joey/dev/mongodb/demo/node_modules/mongodb/lib/collection.js:1889:10)
    at col.insert.then (/Users/joey/dev/mongodb/demo/index.js:33:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  name: 'MongoError',
  message: 'Use of expired sessions is not permitted' }

最佳答案

我想我发现了我的代码有什么问题。我需要将 session.endSession(); 移动到 then 或 catch,因为它是一个 promise 调用。

关于node.js - 在 mongodb nodejs 应用程序上使用客户端 session 时出现 `Use of expired sessions is not permitted` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807724/

相关文章:

node.js - 我的应用程序的分布式数据库

node.js - 检查空值快速验证器

json - 使用 JSON 作为存储/传输格式的数据库

mongodb - 如何根据在文档上添加2个属性的值来搜索文档?

mongodb - 带有 docker-compose 的 AdminMongo 不起作用

ruby-on-rails - Mongoid 使用 has_one 将一个对象与同一类的两个不同对象相关联

node.js - 使用 Node js 从 Oracle AQ 中提取消息

node.js - 如何使用 Yarn 安装许多全局包的列表

javascript - 是否可以在没有相对路径的情况下从项目目录外部请求模块?

node.js - Mongoose 查询返回空