node.js - 首次关闭后无法重用 node-mongodb-native 客户端
问题描述
我正在尝试在Nodejs上编写一个使用Mongo本机客户端的小类,但是当我尝试关闭连接然后再次打开(使用相同的客户端对象)时,操作失败,说连接已关闭(但我刚刚打开)。
那么发生了什么?
const { MongoClient } = require('mongodb');
let client = new MongoClient(urlMongoDbInstance)
await client.connect();
console.log(await client.db.(dbName).collection().findOne({}));
// [CONSOLE]
// prints object
client.close();
关闭连接后,我尝试打开它。
await client.connect();
/* [CONSOLE]
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [mode] is not supported
the options [tags] is not supported
the options [hedge] is not supported
the options [preference] is not supported
the options [isValid] is not supported
the options [slaveOk] is not supported
the options [equals] is not supported
the options [toJSON] is not supported
*/
然后尝试使用数据库连接
console.log(await client.db.(dbName).collection().findOne({}));
// [CONSOLE]
// MongoError: Topology is closed, please connect
我知道(或认为我知道)或发现的
我知道在应用程序运行时保持连接打开是一个很好的做法,但不是这样。
我试图创建一个新变量来每次都连接并工作(但这不是这里的想法)。
我在 Mongo 上找到了一个关于它的条目(似乎是同样的问题)https://jira.mongodb.org/browse/NODE-2544
问题?
有没有人找到解决方法,也许我没有像必须使用的那样使用驱动程序?
解决方案
看起来这是节点驱动程序中的一个已知问题。
您应该能够通过从头开始重新创建客户端对象(而不是尝试重新连接已断开连接的现有客户端)来解决它。
推荐阅读
- python - 如何将神经网络的输出转换为概率?
- android - 未定义对 Callstack 库的引用的问题
- rabbitmq - 使用 RabbitMQ 创建自定义交换以过滤位置
- php - 不好:[php] foreach?
- c# - 使用自定义着色器会导致黑色图像
- javascript - 如何在javascript中将值/参数传递给地图函数
- linkedin - 一级连接 API/SDK
- python - 材料导数或提取/获取向量分量(sympy向量模块)
- laravel - 如何在 laravel 中使用两个不同的 404 错误页面
- html - response.text() 不包含我所期望的