首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoTopologyClosedError:当插入文档或查找文档时,拓扑将关闭

MongoTopologyClosedError:当插入文档或查找文档时,拓扑将关闭
EN

Stack Overflow用户
提问于 2022-05-07 14:52:06
回答 1查看 124关注 0票数 0

我的mongodb集群似乎出了问题,当我试图插入文档或查找文档或在db上执行任何操作时,都会在我的节点控制台中得到这个错误。

代码语言:javascript
运行
复制
MongoTopologyClosedError: Topology is closed
    at processWaitQueue (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\sdam\topology.js:591:46)
    at Topology.selectServer (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\sdam\topology.js:323:9)
    at executeWithServerSelection (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\operations\execute_operation.js:138:14)
    at C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\operations\execute_operation.js:53:13
    at maybePromise (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\utils.js:409:5)
    at executeOperation (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\operations\execute_operation.js:16:37)
    at FindCursor._initialize (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\cursor\find_cursor.js:54:50)
    at FindCursor.[kInit] (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\cursor\abstract_cursor.js:437:14)
    at next (C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\cursor\abstract_cursor.js:498:22)
    at C:\Users\luthe\Desktop\hydrofarm\node_modules\mongodb\lib\cursor\abstract_cursor.js:181:13 {
  [Symbol(errorLabels)]: Set {}

我看到了一些问题的答案,但它们没有解决我的问题,下面是我的db连接设置,希望有人能告诉我我遗漏了什么,是的,我会把我的密码移动到ENV变量,稍后我正在设置数据库:)所以现在我已经掩盖了它。

代码语言:javascript
运行
复制
const { MongoClient, ServerApiVersion } = require('mongodb');
const password = 'xxxxxxxxx';

export const storeData = async (data) =>{
   
const uri = `mongodb+srv://plantmaster:<${password}>@cluster0.yey8l.mongodb.net/plantstore?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
const newPLantData = { name: "Company Inc", address: "Highway 37" };

    client.connect(err => {
    const result = client.db("plantstore").collection("plantdata").insertOne(newPLantData, (err, res) =>{
        if(err) throw err;
 
    client.close();
    return result
})
    })
};


export const getData = async () =>{
   
    const uri = `mongodb+srv://plantmaster:<${password}>@cluster0.yey8l.mongodb.net/plantstore?retryWrites=true&w=majority`;
    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
    
        client.connect(err => {
            client.db("plantstore").collection("plantdata").findOne({lighting:"120"}, function(err, result) {
                if (err) throw err;
                console.log(result.name);
                return result
                db.close();
              });
     
        client.close();
        })    
    };
EN

回答 1

Stack Overflow用户

发布于 2022-05-07 19:08:51

我能够解决这个问题,方法是先建立连接,然后以异步模式运行查询。

代码语言:javascript
运行
复制
   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)
        })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72153632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档