MongoDB在没有NodeJS响应的情况下挂起。

huangapple go评论54阅读模式
英文:

MongoDB hangs without NodeJS Response

问题

我正在尝试从NodeJS运行MongoDB服务器,但我在连接到我的服务器时遇到问题。我已经使用mongod启动了服务器,但当我尝试使用下面的代码连接时,终端会一直挂起,我没有收到“成功连接到服务器”的响应。

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://127.0.0.1:27017'; //localhost

// Database Name
const dbName = 'fruitsDB';

// Create a new MongoClient
const client = new MongoClient(url, { useNewUrlParser: true });

// Use connect method to connect to the Server
client.connect(function(err) {
  assert.equal(null, err);
  console.log("成功连接到服务器");

  const db = client.db(dbName);

  client.close();
});

我已确认我的数据库通过mongosh在运行。当我在url中将127.0.0.1替换为“localhost”并尝试连接时,我会收到“错误:MongoNetworkError: connect ECONNREFUSED ::1:27017”的错误消息,这就是我切换到在url中使用127.0.0.1的原因。

是否有人可以帮助解决这个问题?提前感谢!

英文:

I am trying to run a mongoDB server from NodeJS and I am having issues connecting to my server. I have started the server using mongod, but when I try to connect using the code below, the terminal just hangs and I do not get a "Connected successfully to server" response.


const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://127.0.0.1:27017'; //localhost

// Database Name
const dbName = 'fruitsDB';

// Create a new MongoClient
const client = new MongoClient(url, {useNewUrlParser: true });

// Use connect method to connect to the Server
client.connect(function(err) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

I have confirmed that my db is running through mongosh. When I substitute the 127.0.0.1 for "localhost" in the url and try to connect, I get a "error: MongoNetworkError: connect ECONNREFUSED ::1:27017", which is why I switched to using 127.0.0.1 in the url.

Can anyone please help with this? Thanks in advance!

答案1

得分: 0

根据精细手册MongoClient.connect() 返回一个 Promise(看起来回调已被弃用)。

此外,从 v4.7 开始,显式调用 connect() 已变为可选。

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// 连接 URL
const url = 'mongodb://127.0.0.1:27017'; //localhost

// 数据库名称
const dbName = 'fruitsDB';

// 创建一个新的 MongoClient
const client = new MongoClient(url, {useNewUrlParser: true });

// 创建一个异步的 IIFE 包装器,以便我们可以使用 `await`
void async function() {
  await client.connect();
  console.log("成功连接到服务器");

  const db = client.db(dbName);
  ...
  await client.close();
}();

(我省略了错误处理,但你可以使用 try/catch 处理错误)。

英文:

According to the fine manual, MongoClient.connect() returns a Promise (it looks like callbacks have been deprecated).

Also, explicitly calling connect() has been made optional in v4.7.

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://127.0.0.1:27017'; //localhost

// Database Name
const dbName = 'fruitsDB';

// Create a new MongoClient
const client = new MongoClient(url, {useNewUrlParser: true });

// Create an async IIFE wrapper so we can use `await`
void async function() {
  await client.connect();
  console.log("Connected successfully to server");

  const db = client.db(dbName);
  ...
  await client.close();
}();

(I left out error handling, but you'd use try/catch for that).

huangapple
  • 本文由 发表于 2023年6月27日 21:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定