【发布时间】: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