Mongodb Docker init configuration

108
January 15, 2021, at 9:00 PM

I have problems with my docker-mongodb configuration. When I try to access my mongodb's collection through mongoose, mongo returns the following error:

(base) c27gc@c27gc-desktop:~/Documentos/Code/Development/bloomtasks$ node usecases/get.js 
Testing Connection...
Connection Error: MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/c27gc/Documentos/Code/Development/bloomtasks/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/home/c27gc/Documentos/Code/Development/bloomtasks/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/c27gc/Documentos/Code/Development/bloomtasks/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:719:22)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed'
}

The file get.js

'use strict';

const mongoose = require('mongoose');
const Repository = require('../Interfaces/repository.js')
const Task = require('../models/task.js');
const User = require('../models/user.js');
const date = require('date-and-time');
const path = 'mongodb://username:password@localhost:27017/dbname'
const repository = new Repository(path)
const now = new Date();
repository.create(Task, {
    name: "My Name",
    description: "Test String",
    project: "My Project's Name",
    difficulty: 2,
    startDate: date.format(now, 'ddd, MMM DD YYYY'),
    maxDevelopmentTime: 10,
    expirationDate: date.format(now, 'ddd, MMM DD YYYY'),
    comments: "String",
    deliveryDate: date.format(now, 'ddd, MMM DD YYYY'),
    assigned: false,
    priority: 1,
    reward: 15,
    technologies: ["React", "Nodejs"]
})

Class Repository definition:

'use strict';
const mongoose = require('mongoose');

class Repository {
    constructor(path){
        mongoose.connect(path, {useNewUrlParser: true, useUnifiedTopology: true});
        console.log("Testing Connection...")
        const connection = mongoose.connection;
        connection.on('error', console.error.bind(console, 'Connection Error:'));
    }
    create(model, payload) {
        model.create(payload)
        mongoose.connection.close()
    }
}
module.exports = Repository

When I change the following line of get.js

const path = 'mongodb://username:password@localhost:27017/dbname'

for

const path = 'mongodb://username:password@localhost:27017/'

This creates a db named "test" and inside of it the collection "tasks". I believe that this error is probably caused by my docker-mongodb setting. docker-mongodb setting:

docker-compose.yml

version: "3.8"
services:
  database:
    image: 'mongo'
    container_name: 'mongo-test'
    environment:
      - MONGO_INITDB_DATABASE=dbname
      - MONGO_INITDB_ROOT_USERNAME=username
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
      - ./mongodata:/data/db
    ports:
      - '27017-27019:27017-27019'

init-mongo.js first try:

databases = ['dbname']
for (var i = databases.length - 1; i >= 0; i--) {
  db = db.getSiblingDB(databases[i])
  db.createUser({
    user: "username",
    pwd: "password",
    roles: ["readWrite"]
  })
}

second try:

db.createUser({
    user: "username",
    pwd: "password",
    roles: [
    {
        "role" : "readWrite",
        "db" : "dbname"
    }]
  })

Maybe, the init-mongo.js file has not been reconized. Thanks for your help.

READ ALSO
$_SESSION is not updating with Ajax

$_SESSION is not updating with Ajax

I'm trying to update the session playerPlan but instead of giving me the updated plan value, it gives me 1, which was the preset valueI suspect that the PHP is not recognizing my post but my other PHP file was recognizing my text input posts

116
working but need to get that piece from mysql

working but need to get that piece from mysql

I have the following code which is working as expected,

121
error: selenium.common.exceptions.TimeoutException: Message:

error: selenium.common.exceptions.TimeoutException: Message:

in the code below, I tried to log in to a website using selenium But I get this error seleniumcommon

119
jetpack compose not responding to keyboard input on emulator

jetpack compose not responding to keyboard input on emulator

The software keyboard works while keyboard has no response

205