无法在 heroku 上使用 node.js 连接到 mongodb-atlas
can't connect to mongodb-atlas with node.js on heroku
无法从部署到 Heroku 的节点应用向 MongoDB Atlas DB
发出请求。它在我的本地主机上运行良好,但在生产中存在一些问题。
我将我的连接 URI 添加到 Heroku 中的配置变量中:
MONGODB_URI mongodb+srv://asd:asdasd@cluster0-rdame.mongodb.net/myDb?retryWrites=true&w=majority
const MongoClient = require("mongodb").MongoClient;
const client = await new MongoClient(process.env.MONGODB_URI,{ useNewUrlParser: true});
client.connect();
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
当我执行一些获取或 post 请求时,控制台会给我这个:
2019-08-08T08:32:02.811420+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/branches" host=mighty-sierra-48879.herokuapp.com request_id=0b3c6788-644b-4d9c-87ac-49edee07ba1c fwd="109.75.35.153" dyno=web.1 connect=1ms service=30000ms stat
us=503 bytes=0 protocol=https
好的,我找到了解决办法。
它没有连接到数据库,因为我的代码是错误的
我写
mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true});
const conn = mongoose.connection;
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
而不是:
const MongoClient = require("mongodb").MongoClient;
const client = await new MongoClient(process.env.MONGODB_URI,{ useNewUrlParser: true});
client.connect();
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
一切正常
无法从部署到 Heroku 的节点应用向 MongoDB Atlas DB
发出请求。它在我的本地主机上运行良好,但在生产中存在一些问题。
我将我的连接 URI 添加到 Heroku 中的配置变量中:
MONGODB_URI mongodb+srv://asd:asdasd@cluster0-rdame.mongodb.net/myDb?retryWrites=true&w=majority
const MongoClient = require("mongodb").MongoClient;
const client = await new MongoClient(process.env.MONGODB_URI,{ useNewUrlParser: true});
client.connect();
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
当我执行一些获取或 post 请求时,控制台会给我这个:
2019-08-08T08:32:02.811420+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/branches" host=mighty-sierra-48879.herokuapp.com request_id=0b3c6788-644b-4d9c-87ac-49edee07ba1c fwd="109.75.35.153" dyno=web.1 connect=1ms service=30000ms stat us=503 bytes=0 protocol=https
好的,我找到了解决办法。 它没有连接到数据库,因为我的代码是错误的
我写
mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true});
const conn = mongoose.connection;
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
而不是:
const MongoClient = require("mongodb").MongoClient;
const client = await new MongoClient(process.env.MONGODB_URI,{ useNewUrlParser: true});
client.connect();
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
一切正常