简体   繁体   English

mongodb node.js 客户端,连接挂起

[英]mongodb node.js client, connect hangs

node-mongodb-native node.js client hangs when MongoClient.connect(...) , but mongodb-client (shell command line) works on terminal. node-mongodb-native node.js 客户端在MongoClient.connect(...)时挂起,但mongodb-client (shell command line)在终端上工作。 Any clues?有什么线索吗?

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect(
    'mongodb://my.mongo.db.server.ip:27017/test',
     function(err, db) {

        if(err) throw err;
        console.log("shows during connect call back");

        });

// When load into node shell, it hangs forever

It's been a long time since this question has been asked, but I'll post an answer for those wishing to use mongodb as opposed to mongoose or mongojs (at the time of this writing mongojs depends on an older insecure version of the mongodb driver).这是一个很长的时间,因为这个问题已经被问过,但我会后回答为那些希望使用那些mongodb ,而不是mongoosemongojs (在本文写作mongojs的时间对MongoDB的驱动程序的早期不安全的版本取决于) .

TL;DR Version TL;DR 版本

The program executes normally, but adding the line db.close();程序正常执行,但添加了一行db.close(); will allow your program to terminate normally:将允许您的程序正常终止:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect(
    'mongodb://my.mongo.db.server.ip:27017/test',
     function(err, db) {
        if(err) throw err;
        console.log("shows during connect call back");
        db.close(); //call this when you are done.
        });

Why Node Appears to Hang When Using mongodb.connect()为什么节点在使用mongodb.connect()时出现挂起

As explained in this answer , node will not exit when it has a callback waiting for an event.本答案中所述,当节点有等待事件的回调时,它不会退出。

In this case, connect() registers a callback that waits for the event 'close' to be emitted, indicating that all database connections have been closed.在这种情况下, connect()注册一个回调,等待事件'close'发出,表明所有数据库连接都已关闭。 This is why unless you call db.close() , your script will appear to hang.这就是为什么除非您调用db.close() ,否则您的脚本似乎会挂起。 Note however, everything you code will execute, your program will just not terminate normally .但是请注意,您编写的所有代码都将执行,您的程序将不会正常终止

An Example一个例子

To demonstrate, if you put the following code block into a file called connect.js ...为了演示,如果您将以下代码块放入名为connect.js的文件中...

const MongoClient = require('mongodb').MongoClient;
async function dbconnect() {
console.log("This will print.");

const db = await MongoClient.connect(
    'mongodb://my.mongo.db.server.ip:27017/test');

console.log("This will print too!");

And execute it within a terminal...并在终端内执行它...

$ node connect.js

the result will be:结果将是:

$ node connect.js
This will print.
This will print too!

You will get no further command line prompts.您将不会收到进一步的命令行提示。

In conclusion remember to close your database connections, and all will be well in the world!总之记得关闭你的数据库连接,一切都会好起来的!

For anyone else facing a similar issue, all I had to do was add a .catch and it worked fine from there on:对于面临类似问题的任何其他人,我所要做的就是添加一个.catch并且从那里开始工作正常:

const mongodb = require("mongodb");

const connectDb = mongodb.MongoClient.connect(process.env.MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).catch(err =>
    res.status(400).json({ msg: `Could not connect to MongoDB`, err })
);

module.exports = connectDb;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM