[英]I tried to get data from mongodb atlas
我正在使用它来连接 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.
我找到了解决方案,因为我关闭计算机并再次打开它,我的 IP 地址也更改了,但我忘记更改 Mongodb 图集中的网络访问 IP 地址。 因为这只是一种实践,所以现在我将网络访问 IP 地址更改为公共地址,所以我再也不会遇到这个问题了,请在此处输入图片描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.