【问题标题】:I tried to get data from mongodb atlas我试图从 mongodb atlas 获取数据
【发布时间】:2020-09-27 13:08:28
【问题描述】:

我正在使用它来连接 mongodb。在以前我已经完美地运行它但是在我关闭终端并运行它之后我得到了错误。

+这是我的代码

async function loadTasksCollection(){
    const url="mongodb+srv://user:password@mytasklist.a75ix.mongodb.net/mytasklist?retryWrites=true&w=majority";
    const client=new MongoClient(url,{useNewUrlParser:true,useUnifiedTopology: true});
    try {
        await client.connect();
        console.log("sucess")
    } catch (error) {
        console.log(error);
        
    }
    return client.db("tasklist").collection("task");
    
    
}

// to read the task
router.get('/',async(req,res)=>{  
    const task= await loadTasksCollection()
    res.send(await task.find({}).toArray())
    
});

+这是第一个错误

    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'mytasklist-shard-00-02.a75ix.mongodb.net:27017' => [ServerDescription],
      'mytasklist-shard-00-00.a75ix.mongodb.net:27017' => [ServerDescription],
      'mytasklist-shard-00-01.a75ix.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

+几秒钟后我明白了

(node:6213) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6213) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

    标签: node.js mongodb api express es6-promise


    【解决方案1】:

    我找到了解决方案,因为我关闭计算机并再次打开它,我的 IP 地址也发生了变化,但我忘记在 Mongodb atlas 中更改网络访问 IP 地址。因为这只是一种做法,所以现在我将网络访问 IP 地址更改为公共地址,这样我就再也不会遇到这个问题了 enter image description here

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2019-09-15
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多