如何解决MongoDb UnhandledPromiseRejectionWarning:错误:queryTxt ETIMEOUT cluster0.3zwxl.mongodb.net?

时间:2020-08-27 13:24:41

标签: javascript node.js database mongodb mongoose

我能够启动服务器,但无法以localhost身份运行Web应用程序,并出现以下错误

Server started on port 3000
(node:13052) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0.3zwxl.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19)
(node:13052) 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:13052) [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.

这是我的网址

mongoose.connect(“ mongodb + srv:// xxxxxxxx:vikas@cluster0.3zwxl.mongodb.net/xxxxxx”,{useNewUrlParser:true,useUnifiedTopology:true});

此外,Web应用程序未加载并显示“无法访问此网站”

2 个答案:

答案 0 :(得分:0)

我已经解决了问题

mongoose.connect("mongodb+srv://xxxxxxxx:xxxxxxxx@cluster0.3zwxl.mongodb.net/xxxxxx?retryWrites=true&w=majority",{useNewUrlParser:true,useUnifiedTopology: true});

mongoose.connection.on("connected",()=>{
console.log("Mongoose connected");
})

答案 1 :(得分:0)

更好的多种连接方式,供将来使用

    const connectDB = async () => {
        try {
          await mongoose.connect(process.env.MONGO_URI, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: false,
          });
          console.log("MONGODB: Default Connection Established");
        } catch (error) {
          console.log(error);
          process.exit(1);
        }   
     }

   connectDB();