Mongoose.js creates multiple connections to MongoDB from one connect() call

前端 未结 2 1305
醉梦人生
醉梦人生 2020-12-16 13:24

I am making a single connection to MongoDB via Mongoose in Node.js Express app:

var express = require(\'express\');
var mongoose = require(\'mongoose\');
mon         


        
2条回答
  •  鱼传尺愫
    2020-12-16 13:54

    Add this if statement to check only when mongoose disconnected, try to connect it

    if (Mongoose.connection.readyState === 0)
           mongoose.connect('localhost', 'test');
    

    readyState contain these types :

    0: disconnected
    1: connected
    2: connecting
    3: disconnecting
    

提交回复
热议问题