node.js - Mongo运行时错误: Connection pool closed

标签 node.js mongodb api

我看到我的 mongoose 池在它插入数据之前似乎关闭了,因为我在调用我的云集群中的 mongoose 数据库时遇到此错误

MongoRuntimeError: Connection pool closed

但我正在等待所有电话?所以我不确定为什么会看到这个问题,也许这与我定义客户的方式有关?希望有人对此有一些提示或想法

export const storeData = async (data) =>{
    const uri = `mongodb+srv://plantmaster:${password}@cluster0.yey8l.mongodb.net/plantstore?retryWrites=true&w=majority`;
    const client = await MongoClient.connect(uri, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        serverApi: ServerApiVersion.v1
      });

        const newPLantData = { name: "Company Inc", address: "Highway 37" };
        await client.db("plantstore").collection("plantdata").insertOne(newPLantData, (err, res) =>{
            if(err) throw err;
            console.log(result)
        })
       
        await client.close();
};

我像这样在 express 邮寄路线上调用这个函数

// store data
app.post('/store', async function (req, res) {
 await storeData(req.body);
  res.send('data was stored')
})

最佳答案

我遇到了同样的错误,但我通过在关闭连接前等待 1500 毫秒来修复它。

setTimeout(() => {client.close()}, 1500)

关于node.js - Mongo运行时错误: Connection pool closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72155712/

相关文章:

javascript - 在 Node 中的客户端和服务器之间共享变量

node.js - 蒙戈 : "2d" index and normal index

mongodb - 在 MongoDB 聚合中加入两个数组?

python - 如何根据 FastApi 中的另一个查询参数使查询参数成为必需的?

C# 和 Ghostscript 64 位

api - Slack API - 固定消息

javascript - 如何在 Redis 记录中添加 unix 时间戳作为过期时间?

javascript - 某些 IDE 的异步和等待错误

node.js - 使用 Sequelize 获取 360 万条记录会使 Node 崩溃,MariaDB 连接器可以工作。知道为什么吗?

node.js - 在 SailsJS 中维护模型更新历史