I am learning docker so I was following one of the video it shown that how to connect your js app with mongoDB when it is running in docker containers . Now little variation I did is that it shown to connect mongo on system port 27017 which is default mongo port but the problem is I don’t want to connect to my system installed mongo but to the container mongo , so for that purpose I decided to run mongo container on port 3535. When I did so mongo-express successfully got connected to mongo but when I am trying to connect it with my js app(using mongoose ) it is continuously showing errors with authentication error I have checked password is correct , no brackets issue is their , the command I used to turn up my docker containers are
docker run -d -p 3535:27017 --name mongoDB --network databases -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin mongo
and another one for mongo-express is
docker run -d -p 8081:8081 --name mongoExp --network databases -e ME_CONFIG_BASICAUTH_USERNAME=admin -e ME_CONFIG_BASICAUTH_PASSWORD=admin -e ME_CONFIG_MONGODB_SERVER=mongoDB -e ME_CONFIG_MONGODB_AUTH_USERNAME=admin -e ME_CONFIG_MONGODB_AUTH_PASSWORD=admin mongo-express
My node app code looks like
const mongoose = require('mongoose');
const {Humans}= require("./models/humans.js")
mongoose.connect('mongodb://admin:admin@localhost:3535/user', {useNewUrlParser: true, useUnifiedTopology: true }).then(()=>{
console.log("connected to db");
})
let data={
name:"Vaibhav Jain",
mob:"9801",
skills:"coding"
}
const express = require('express')
const app = express();
app.set('view engine', 'ejs');
app.get("/",async(req,res)=>{
// console.log(Humans);
// await Humans.insertMany( );
// await Humans.insertMany({name:"Mobius" , mob:"5908922",skills:"drawing"});
const result = await Humans.find();
res.send(result);
})
app.get("/home",(req,res)=>{
res.render("home", data);
})
app.listen(3000,()=>{
console.log("listening port");
})
Also one thing i would like to mention I have created a user database which have a collection human , this all I created from UI but I wanted to connect it to my app using mongoose , so for that Human which is shown to be imported is actually empty schema (read that from somewhere that you can use existing collections in such a way please correct me on this part also if I am wrong )