无法连接到 mongoDB 地图集

Not able to connect to mongoDB atlas

我已正确安装所有内容,但不知道发生了什么。这是代码

// Connect to MongoDB
const dbURI = 'mongodb+srv://<username>:<password>@blogging-site.mantx.mongodb.net/Blogging-site?retryWrites=true&w=majority';
try {
  // Connect to the MongoDB cluster
   mongoose.connect(dbURI,{ useNewUrlParser: true, useUnifiedTopology: true },
    () => console.log(" Mongoose is connected")
  );
} catch (e) {
  console.log("could not connect");
}

我的 VS 代码终端中的错误是 - 错误:queryTxt ETIMEOUT 博客 -site.mantx.mongodb.net
在 QueryReqWrap.onresolve [完成时] (dns.js:203:19) { 错误号:未定义, 代码:'ETIMEOUT', 系统调用:'queryTxt', 主机名:'blogging-site.mantx.mongodb.net' } 我正在关注 net Ninja 教程,希望我没有按照他的指示离开任何一步。 我之前使用过 mongoDB atlas 并在我的计算机上安装了 mongodb。这会造成任何问题吗?

'I could connect'没问题

我可以连接到它

我,一个你不认识的人,一个大概在世界另一端的人

可能是一个有恶意的人(虽然我不是)。

这可能是连接问题(由于网络不好或其他原因),但更大的问题是

我可以连接到它

所以也许你想在 public 之前将 URI 部分留下来,然后让每个人都看到,除非你想让坏人窃取用户信息、会话数据、还有什么不是来自您的数据库。

学习愉快!!!

评论后编辑 以下是开始使用 mongoose 应该学习的内容:

  -setting up initial(also called default) connection with mongoose.connect()
  -configuring schemas with mongoose.Schema()
  -compiling models with schemas with mongoose.model()
  -saving the doc with mongoose.save()
  -querying the database(findOne, find, deleteOne, updateOne, findById are the 
   ones I find most used)

也许当你习惯了所有这些之后,你可以开始学习填充(你可能会在更大的项目中需要它)、创建多个连接和导出模式模式、验证、mongooseJs 的中间件等等我相信你会自己调查的。

至于“你能告诉我你是如何连接到地图集的吗?”:

我刚刚将您的代码和 运行 复制到我的计算机上。就那么简单。 dbURI 变量中的该字符串是连接到数据库图集的一种方式。你给我的,我用了,连上了。

Error: queryTxt ETIMEOUT

当客户端尝试从 DNS 检索 blogging-site.mantx.mongodb.net 的 TXT 记录时超时。

在同一台机器上,使用 nslookup 进行测试以检索 TXT 记录,如果您的 DNS 提供商不支持,您将需要使用非 SRV 连接字符串。