skip to Main Content

I am trying to dockerize my node.js app and it just won’t work. I narrowed the point of error down to two lines, but I just can’t get to the ground of the problem. I’ll share my code if you need more just ask for it. I am using a raspberry pi 3 to deploy my container unsing portainer. Maybe I missed a setting there idk.

console.log("Hello from the docker3")

//Database
const mongoose = require("mongoose");

mongoose.connect(
  `mongodb+srv://{process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_ADDRESS}/`
);
console.log("Hello from the docker4")

The rest of the code are just some webpages that get sent and some express.js stuff.

My error

I am getting this error from this part, you can see that the print number 4 doesn’t get printed so I think it’s somewhere there

I searched everywhere but I couldn’t find a reason for this.

2

Answers


  1. Can you modify your connection code to log any errors that occur during the connection process. This can provide more insight into what’s happening :

    const mongoose = require("mongoose");
    console.log("Hello from the docker3");
    
    mongoose.connect(
      `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_ADDRESS}/`,
      { useNewUrlParser: true, useUnifiedTopology: true }
    ).then(() => {
      console.log("Connected to MongoDB successfully");
    }).catch((error) => {
      console.error("Error connecting to MongoDB:", error);
    });
    
    Login or Signup to reply.
  2. mangoose return promise object and if we want to get response from promise object either we have to use await or then Try following ways

    try {
        await mongoose.connect(
            mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_ADDRESS}/
        );
        console.log("Connected to MongoDB");
    } catch (error) {
        console.error("Failed to connect to MongoDB:", error);
    }
    

    Ensure that the environment variables (DB_USER, DB_PASSWORD, DB_ADDRESS) are set correctly. You can print these values to the console to verify if they are being correctly retrieved from the environment:

    console.log("DB_USER:", process.env.DB_USER);
    console.log("DB_PASSWORD:", process.env.DB_PASSWORD);
    console.log("DB_ADDRESS:", process.env.DB_ADDRESS);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search