首页 > 解决方案 > MongoDB Atlas 连接在 Heroku for NodeJS 上不起作用

问题描述

我在 Heroku 上不断收到此错误:

UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/

我已经将 0.0.0.0 和我自己的 IP 地址添加到 Atlas。

这就是我连接的方式(使用猫鼬):

// link mongoose
mongoose.connect(process.env.DB_LINK, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
});

我已经将 DB_LINK 添加到 Heroku 上的配置变量中。

一切都在本地主机上运行良好。我不确定为什么这不起作用。最后我也收到此错误:

at=error code=H12 desc="Request timeout"

感谢您的帮助!

标签: node.jsmongodbherokumongoosemongodb-atlas

解决方案


好的,我会按照@Jack 评论部分的描述给出更详细的答案。

1. 转到网络访问部分 在此处输入图像描述

2.点击add IP address右侧的按钮 在此处输入图像描述

3. 单击允许从任何地方访问。 在此处输入图像描述


推荐阅读