GitHub Documentation Patreon donate button

process.env.MONGO_URI not working as expected in colyseus social package



  • I am trying to use colyseus social package and I set MONGO_URI as expected via process.env.MONGO_URI in my index.ts but while colyseus server going to make a connection with mongodb it is getting default value(old value) although process.env.MONGO_URI updated to new value.

    I tried to check it in connectDatabase method of index.ts in social module and console like :-

    import { MONGO_URI } from "./env";

    export async function connectDatabase(cb?: (err: MongoError) => void) {
    // skip if already connecting or connected.
    if (mongoose.connection.readyState !== 0) {
    if (cb) cb(null);
    return;
    }

    try {
        console.log(process.env.MONGO_URI);// getting updated value
        console.log(MONGO_URI);//getting old value
        await mongoose.connect(MONGO_URI, { autoIndex: false, useNewUrlParser: true }, cb);
        debug(`Successfully connected to ${MONGO_URI}`)
    
        // reconnect if disconnected.
        mongoose.connection.on('disconnected', () => connectDatabase());
    } catch (e) {
        console.error('Error connecting to database: ', e);
    }
    

    }

    Please help it out . Thanks in advance


  • administrator

    Hi @Viveksharma, are you setting the MONGO_URI environment variable before importing the @colyseus/social module?

    It's usually a good practice to put require('dotenv').config() as the very first line in your server entry file: (using https://www.npmjs.com/package/dotenv)

    Cheers



  • @endel thanks .I have import @colyseus/social in my index.ts before dotenv


 

GitHub Documentation Patreon donate button

© 2019 Endel Dreyer