【发布时间】:2019-08-02 17:59:23
【问题描述】:
我有一个本地 Angular 项目,连接到我的 mongodb,但是当尝试连接到 mongodb.com 上的远程项目时,我收到未处理的承诺异常:
(node:23) UnhandledPromiseRejectionWarning: ReferenceError: err is not defined at mongoose.connect.then.catch (/app/app.js:27:26)
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().
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.
此代码似乎有问题,但我不确定要更改什么,因为这在本地有效。在 heroku 上运行时,出现错误。
// Map global promise - get rid of warning
mongoose.Promise = global.Promise;
// Connect to mongoose
mongoose.connect("mongodb+srv://<username>:<password>@cluster0-cx2dj.mongodb.net/test?retryWrites=true", {
useNewUrlParser: true
})
.then(() => console.log('MongoDb Connected...'))
.catch(() => console.log(err));
【问题讨论】:
-
不同版本的Node?此外,您只会看到 is 连接错误,您是否在本地调用过该错误?
-
啊,我明白了。如果我在本地断开与 mongodb 的连接,我会收到该错误。我不知道为什么我可以在本地连接但不能通过 Heroku。
-
如果您确实将
err添加到.catch回调参数中,您可能会看到更多信息。 -
感谢您的建议,我是 Angular 新手,错过了这个错误。添加对 err 的缺失引用告诉我一个错误,我可以更好地调试。请看我的回答。
标签: node.js angular mongodb heroku