[英]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 start
或node 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);
}
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.