简体   繁体   English

当我尝试启动我的 Node.js 应用程序时出现错误 UnhandledPromiseRejectionWarning

[英]Error UnhandledPromiseRejectionWarning when I try to launch my Node.js app

My code:我的代码:

const fastify = require('fastify');
const mongoose = require('mongoose');

const app = fastify();

try {
    mongoose.connect('mongodb://localhost:27017/notes_db');
} catch (e) {
    console.error(e);
}

app.get('/', (request, reply) => {
    try{
        reply.send("Olá mundo");
    } catch(e) {
        console.error(e);
    }
})

app.listen(5000, (err, address) => {
    if (err) {
        console.error(err);
        process.exit(1);
    }
    console.log(`Server running on ${address}`);
});

When I run the command npm start or node index.js this error is shown to me:当我运行命令npm startnode index.js这个错误显示给我:

(node:10332) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Server running on http://127.0.0.1:5000
(node:10332) UnhandledPromiseRejectionWarning: Error: Password contains an illegal unescaped character
    at parseConnectionString (C:\Users\mikae\Desktop\notes-server\node_modules\mongodb\lib\url_parser.js:299:13)
    at parseHandler (C:\Users\mikae\Desktop\notes-server\node_modules\mongodb\lib\url_parser.js:130:14)
    at QueryReqWrap.callback (C:\Users\mikae\Desktop\notes-server\node_modules\mongodb\lib\url_parser.js:114:7)
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:205:10)
(node:10332) 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:10332) [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.

My server is running but I want to know how to fix this message.我的服务器正在运行,但我想知道如何修复此消息。

Can you try specifying the useNewUrlParser option like this:您可以尝试像这样指定useNewUrlParser选项吗:

try {
    mongoose.connect('mongodb://localhost:27017/notes_db', { useNewUrlParser: true });
} catch (e) {
    console.error(e);
}

Edit编辑

Since you're trying to connect to MongoDB Atlas cluster and not local installation, replace the connection URI to the one you get on Atlas.由于您尝试连接到 MongoDB Atlas 集群而不是本地安装,请将连接 URI 替换为您在 Atlas 上获得的连接 URI。

Here is a tutorial I found: https://medium.com/@sergio13prez/connecting-to-mongodb-atlas-d1381f184369这是我找到的教程: https://medium.com/@sergio13prez/connecting-to-mongodb-atlas-d1381f184369

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js:当我尝试启动我的应用程序时,我的服务器请求 hot-update.js - Node.js : my server request hot-update.js when I try to launch my application 我在Node.JS应用程序中使用路由,当我尝试从浏览器中打开其中一个路由时,出现错误 - I use routes in my Node.JS app and when I try to open one of the routes from the browser I get an error 我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js] - My Server is up but when I try to connect to it I am getting an error [Node.js] 运行node.js应用程序时出现错误(也使用Rabbit) - I am getting an error when i run my node.js app ( also using rabbit ) Node.js错误:(node:9748)UnhandledPromiseRejectionWarning:未处理的承诺被拒绝 - Node.js ERROR: (node:9748) UnhandledPromiseRejectionWarning: Unhandled promise rejection 在尝试部署 node.js 应用程序时,如何解决“UnhandledPromiseRejectionWarning”错误? - How would one resolve a 'UnhandledPromiseRejectionWarning' error, while trying to deploy a node.js app? 尝试运行Node App时出现错误 - i am getting error when try to run my Node App 尝试使用 PM2 集群启动 node.js 应用程序时出错 - Getting error when trying to launch node.js app with PM2 Cluster 使用node.js从Heroku启动Javascript应用程序时,无法在Facebook选项卡上发布错误 - CANNOT POST error on facebook tab when using node.js to launch Javascript app from Heroku 将 mongoose 与 node.js 连接时,如何修复 UnhandledPromiseRejectionWarning? - How can i fix UnhandledPromiseRejectionWarning when connecting mongoose with node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM