简体   繁体   中英

Mongoose on 'connected' never firing even though MongoDB accepted multiple connections

I've got a local MongoDB server running, and am trying to connect to it using NodeJS:

// set up ========================
var express         = require('express');
...

// configuration =================
var dbURI = 'mongodb://localhost:27017/DataStructureVisualizer'; 

mongoose.connection.on('connecting', function ()    { console.log('MongoDB: Trying: ' + dbURI);                             }); 
mongoose.connection.on('connected', function ()     { console.log('MongoDB: Successfully connected to: ' + dbURI);          }); 
mongoose.connection.on('error',function (err)       { console.log('MongoDB: ERROR connecting to: ' + dbURI + ' - ' + err);  }); 
mongoose.connection.on('close',function (err)       { console.log('MongoDB: Connection Closed');                            }); 
mongoose.connection.on('reconnected', function ()   { console.log('MongoDB: Database link was reconnected');                });
mongoose.connection.on('disconnected', function ()  { console.log('MongoDB: The connection was ended on: ' + dbURI );       });
mongoose.connection.on('open', function(ref)        { console.log("Connected to mongo server.");                            });
mongoose.connect(dbURI);

The 'connecting' function fires, and outputs in the shell running the NodeJS server. On the MongoDB side, it outputs:

2017-11-28T20:45:12.218-0600 I NETWORK  [thread1] waiting for connections on port 27017
2017-11-28T20:45:23.913-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:56365 #1 (1 connection now open)
2017-11-28T20:45:23.922-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:56366 #2 (2 connections now open)
2017-11-28T20:45:23.925-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:56367 #3 (3 connections now open)
2017-11-28T20:45:23.929-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:56368 #4 (4 connections now open)
2017-11-28T20:45:23.930-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:56369 #5 (5 connections now open)

Why is it opening five connections? And why is disconnected being called when I close the MongoDB server but connected isn't?

显然,我使用的是猫鼬(3.6.2)的非常旧的版本,并将其更新为4.13.6可以缓解此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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