[英]nodejs cant connect to mongodb using mongoose
所以我所有的应用程序都使用 mongoose 工作得很好,突然间它们都停止了。 我无法连接到我的数据库。 但是我使用 mongoose 数据库部署的应用程序工作正常,但我无法使用本地计算机访问任何数据库。 这是我编写的测试服务器。 有人可以帮我看看发生了什么。
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser')
const app = express();
app.use(express.json())
const db = require('./keys').mongoURI;
mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('connected'))
.catch(err => console.log(err))
const port = process.env.PORT || 5000;
app.listen(port, () => console.log('server running'))
module.exports = {
mongoURI: 'mongodb+srv://ertemishakk:<password>@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majority'
}
终端:服务器运行 MongoTimeoutError:服务器选择在 Timeout._onTimeout (/Users/ishakertem/Desktop/test/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9) 在 30000 毫秒后超时在 listOnTimeout(内部/ timers.js:536:17) 在 processTimers (internal/timers.js:480:7) { name: 'MongoTimeoutError', }
你应该检查你的
bindIp : [public_ip]
选项
mongod.conf
文件。 应该指出运行 MongoDB 的系统的公共 IP。 0.0.0.0 只能用于调试,因为它是暴露的 1。
[请投票给这个答案,如果它对你有帮助]
你可以遵循这个代码我希望能解决你的问题
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser')
const app = express();
app.use(express.json())
//Remove <password> and provide DB password
/**
* Example: mongodb + srv: //ertemishakk:123@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majorit
*/
mongoose.connect('mongodb+srv://ertemishakk:<password>@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majorit', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('connected'))
.catch(err => console.log(err))
const port = process.env.PORT || 5000;
app.listen(port, () => console.log('server running'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.