Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

645
Views
Can't solve MongoParseError: Invalid connection string

I'm not able to solve MongoParseError.

First I had tried this

mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});

made me ending up in this error

MongoParseError: Unescaped slash in userinfo section

Then I escaped all the slashes

mongoose.connect(`mongodb:\\/\\/${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017\\/${process.env.DB_NAME}`, {useNewUrlParser: true});

and I ended up in this error which I'm not able to solve

MongoParseError: Invalid connection string

This is my index.js

'use strict';

const mongoose = require('mongoose');
mongoose.connect(`mongodb:\\/\\/${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017\\/${process.env.DB_NAME}`, {useNewUrlParser: true});

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('ok');
});

const express = require('express');

// Constants
const PORT = 8080;
const HOST = 'localhost';


var kittySchema = new mongoose.Schema({
  name: String
});

var Kitten = mongoose.model('Kitten', kittySchema);

var silence = new Kitten({ name: 'Test1234' });


// App
const app = express();
app.get('/', (req, res) => {
  res.send(silence.name);
});

app.listen(PORT);
console.log(`Running on http://${HOST}:${PORT}`);

And here's the docker-compose.yml

version: '3.7'
services:
  web:
    build: .
    command: npm start
    volumes:
      - ./src:/usr/app/
      - ./src/node_modules:/usr/app/node_modules
    ports:
      - 80:8080
    environment:
      DB_NAME: websitedb
      DB_USER: /run/secrets/db_user
      DB_PASSWORD: /run/secrets/db_password
    secrets:
      - db_user
      - db_password
  db:
    image: mongo:latest
    volumes:
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_DATABASE: websitedb
      MONGO_INITDB_ROOT_USERNAME_FILE: /run/secrets/db_user
      MONGO_INITDB_ROOT_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_user
      - db_password
volumes:
  mongo:
secrets:
  db_user:
    file: ./db_user.txt
  db_password:
    file: ./db_password.txt

Can anyone tell me what I'm doing wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Looking through the guide on managing secrets in docker compose, it looks like any secrets are passed into the container as a file mounted in /run/secrets/SECRET_NAME, and the suggestion is to pass the path to this file as an environment variable and then read the file in your app:

When you deploy, Docker mounts a file under /run/secrets/ in the services. These files are never persisted in disk, but are managed in memory.

Each service uses environment variables to specify where the service should look for that secret data.

You have passed the path to these secrets as process.env.DB_USER, but to get the actual secret you need to read the file:

const fs = require('fs');
const mongoose = require('mongoose');

let dbUser = fs.readFileSync(process.env.DB_USER);
let dbPassword = fs.readFileSync(process.env.DB_PASSWORD);

mongoose.connect(`mongodb://${dbUser}:${dbPassword}@db:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!