我试图从 mongodb atlas 获取数据
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 atlas 中更改网络访问 IP 地址。因为这只是一个练习所以现在我将网络访问 IP 地址更改为 public 所以我再也不会遇到那个问题
enter image description here
我正在使用它来连接 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 atlas 中更改网络访问 IP 地址。因为这只是一个练习所以现在我将网络访问 IP 地址更改为 public 所以我再也不会遇到那个问题 enter image description here