我试图从mongodb图集中获取数据



我正在使用它来连接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())

});

+这是第一个erorr

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.

我找到了解决方案,因为我关闭了我的计算机并再次打开它,我的IP地址也发生了更改,但我忘记更改Mongodb图谱中的网络访问IP地址。因为这只是一个惯例,所以现在我将网络访问IP地址更改为公共地址,这样我就再也不会面临这个问题了在此处输入图像描述

最新更新