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

米凯尔

我的代码:

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}`);
});

当我运行命令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.

我的服务器正在运行,但我想知道如何修复此消息。

多梅尼克

您可以尝试指定这样的useNewUrlParser选项:

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

编辑

由于您尝试连接到 MongoDB Atlas 集群而不是本地安装,请将连接 URI 替换为您在 Atlas 上获得的连接 URI。

这是我找到的教程:https : //medium.com/@sergio13prez/connecting-to-mongodb-atlas-d1381f184369

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试在现有Gradle项目上启动springboot应用程序时出现错误

当我使用Launch4j添加启动画面时,出现启动错误(“启动应用程序时发生错误”)

当我尝试在模拟器android studio上运行应用程序时出现错误

尝试在Heroku上部署Node.js / Express / Socket.io应用程序时出现应用程序错误

当我启动android应用程序时在处理输入流时抛出错误

当我尝试通过GCP deploy命令托管Node.js应用程序时,无法使用。错误:找不到模块“表达”

当我在api大于19的设备上运行我的应用程序时,出现错误

Azure WebApp Linux-运行node.js Express应用程序时权限被拒绝错误

当我尝试在Android设备上预览应用程序时显示错误

从Node.js中的sendGrid发送电子邮件时出现“ UnhandledPromiseRejectionWarning:错误:禁止”

上传我的iOS应用程序时出现错误

启动我的应用程序时出现错误消息

尝试运行Node.js应用程序时出现TypeError

调试Node.js应用程序时WebStorm在错误的文件上停止

当我尝试访问应用程序时出现错误:“ Smalltalk / V”“虚拟机堆栈溢出”

当我尝试在设备中启动我的应用程序时,在xcode中由于启动选项nil出现错误?

当我启动 node.js 服务器时,它启动但出现了一些错误

当我调试 UWP 应用程序时出现与组件 DLL 相关的错误?

当我尝试在 digitalocean 部署我的应用程序时,如何摆脱 ssl 错误?

启动我的 node.js 应用程序时出现错误 connect ECONNREFUSED 127.0.0.1:3306

当我尝试在真实手机上运行我的应用程序时出现“我的应用程序已停止”错误

在尝试部署 node.js 应用程序时,如何解决“UnhandledPromiseRejectionWarning”错误?

当我尝试在 SQLite 中运行我的提交数据应用程序时,出现错误 1 语法错误

当我尝试访问本地主机时,没有调用 node.js 应用程序中的 get()

面对此错误,在启动 react js 应用程序时

尝试安装我的 android 应用程序时收到错误

Node.js - 登录表单错误 UnhandledPromiseRejectionWarning

为什么当我用 node 启动我的机器人时,discord.js 模块会出现错误?

启动快速应用程序时出现错误消息