0

i'm using nodejs and mongoose to connect to my MongoDB Database. I try to check the connection state, if its connected or not. I use mongoose.connection.readyState for this.

The connection works fine (MongoDB Cloud Atlas & my local installation) but the terminal shows me state 0 (disconnected) everytime.

This is my Code: `

const { mongoose } = require('mongoose');

const uriAtlas = "mongodb+srv://<Username>:<Password>@MyClusterAtlas.net/";
const urilocal = "mongodb://127.0.0.1:27017/";

const status = mongoose.connection.readyState;

// Define Delay
const delay = ms => new Promise(res => setTimeout(res, ms));

async function createConnection() {
    mongoose.set('strictQuery', true);
    await mongoose.connect(urilocal,
    // Check Connection state after 5Seconds.
    
    await delay(5000));
    

    // Show connection state
    if (status == 1) {
        console.log("Connected")
    } else {
        console.log("Not Connected!"),
        console.log(`Connection state is: ${status}`);
    }
    };

createConnection();

`

Maybe someone can give me a hint, i searched but doesn't find my error.

I searched and read the documentation but i cant solve my problem. Maybe someone can give me a hint, i think its a little issue.

  • You check the status before you even run `connect`. Move this line `const status = mongoose.connection.readyState;` to the `createConnection` – Konrad Jan 01 '23 at 22:02
  • Thanks for your reply. I tried to set the line `const status = mongoose.connection.readyState;` On the following positions. At the function call `createConnection(console.log(`State: ${mongoose.connection.readyState}));` and in the function declaration ` async function createConnection() { mongoose.set('strictQuery', true); const status = mongoose.connection.readyState; await mongoose.connect(urilocal, // Check Connection state after 5Seconds. ` But it doesn't work for me, it shows still state 0, but the connection is working. – Nico Milano Jan 01 '23 at 22:13

1 Answers1

0
const { mongoose } = require('mongoose');

const uriAtlas = "mongodb+srv://<Username>:<Password>@MyClusterAtlas.net/";
const urilocal = "mongodb://127.0.0.1:27017/";

// Define Delay
const delay = ms => new Promise(res => setTimeout(res, ms));

async function createConnection() {
    mongoose.set('strictQuery', true);
    await mongoose.connect(urilocal,
    // Check Connection state after 5Seconds.
    
    await delay(5000));
    
    const status = mongoose.connection.readyState;

    // Show connection state
    if (status == 1) {
        console.log("Connected")
    } else {
        console.log("Not Connected!"),
        console.log(`Connection state is: ${status}`);
    }
};

createConnection();
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Thanks for your help. I thought i can set the `const status = mongoose.connection.readyState;` on top to declare it for "status". Why i have to declare this inside my function in this case? Maybe i missunderstood something. – Nico Milano Jan 01 '23 at 22:23
  • [Primitive variables are passed by value](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) so when `mongoose.connection.readyState` updates, `status` won't be updated – Konrad Jan 01 '23 at 22:32