I have a problem when trying to connecting my express js with mongodb on docker-compose environment. I have already tried multiple solutions on the internet but it did not solve my problem. This is my express js docker log:
MongoServerError: Authentication failed.
    at Connection.onMessage (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  connectionGeneration: 24,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
This is mongodb log:
Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"tausr","authenticationDatabase":"admin","remote":"172.18.0.9:55028","extraInfo":{},"error":"UserNotFound: Could not find user \"tausr\" for db \"admin\"
This is my docker-compose:
    express:
        build: ./express
        restart: on-failure
        depends_on:
          - mongo
          - couchdb
        env_file: ./.env
        environment:
          - DB_HOST=mongo
          - DB_USERNAME=$MONGODB_USER
          - DB_PASSWORD=$MONGODB_PASSWORD
          - DB_PORT=$MONGODB_DOCKER_PORT
          - DB_NAME=$MONGODB_DATABASE
        ports:
          - "3000:3000"
        links:
         - mongo
        networks:
          - logging
      mongo:
        image: mongo:latest
          #    command: [--auth]
        env_file: ./.env
        environment:
          - MONGO_INITDB_ROOT_USERNAME=$MONGODB_USER
          - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD
            #      MONGO_INITDB_USERNAME: "taUser"
            #      MONGO_INITDB_PASSWORD: "taPass"
          - MONGO_INITDB_DATABASE=$MONGODB_DATABASE
        volumes:
          - ./data:/data/db
            #- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
          ports:
      - '27018:27017'
    networks:
      - logging
This is my .env file
MONGODB_USER=tausr
MONGODB_PASSWORD=tapass
MONGODB_DATABASE=ta
MONGODB_LOCAL_PORT=27018
MONGODB_DOCKER_PORT=27017
NODE_LOCAL_PORT=6868
NODE_DOCKER_PORT=8080
and this is my mongo connection
mongoose.connect(`mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`).then((response) => console.log("connect success")).catch((e) => console.log(e))
 
    