Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

353
Visualizações
MongoError: not authorized on db to execute command? When connection with socket.io

I can't connect to my mongodb from socket.io. When I run my server.js it says that it is connected with mongodb and that it is connected to a socket but then it shows the error

MongoError: not authorized on db to execute command find.

I have loged in as an admin user into the mongo admin database, then went to the "chat" db and there created a new user in the database with the role "readWrite" like this.

use chat

and then:

db.createUser({user: "testuser", pwd: "testuser", roles: [{role: "readWrite", db: "chat" }]})
Successfully added user: {
    "user" : "testuser",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "chat"
        }
    ]
}

The user gets created. And when I test it with db.auth("testuser","testuser") I get a 1. I then exit and go to my bin folder and there I write:mongo.exe -u testuser -p testuser chat;

And then it connects to mongodb://127.0.0.1:127017/chat

I then open a new prompt and go to my folder where I have the server.js that connects with the db and run it, it then first says that the server is running and that it is connected with the mongodb.

When I the go to my chat page and load it then it says that: MongoError not authorized...

I connect my server.js file like this

var port = process.env.PORT || 3000;
server.listen(port);
console.log('Server running *:'+port);

var io = require('socket.io').listen(server);
var mongoose = require('mongoose');

//mongoose.connect('mongodb://127.0.0.1:27017/chat', function(err){
mongoose.connect('mongodb://localhost/chat', function(err){

I don´t know what Im missing, I have followed the docs on mongodb and looked all over the net, but not sure where I go wrong.

Any input really appreciated thanks.

When I load the chat page and look in the console I get this error.

https://manmade.se:3000/socket.io/?EIO=3&transport=polling&t=Lceu0Ot&sid=wbFhDl7yylt4_4a1AAAA net::ERR_CONNECTION_RESET

index_webb.html:1 WebSocket connection to 'wss://manmade.se:3000/socket.io/?EIO=3&transport=websocket&sid=wbFhDl7yylt4_4a1AAAA' failed: WebSocket is closed before the connection is established.
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The error seems to suggest that through your program you are not authenticating on the right database.

When connecting from the shell, in your particular case, this would result in an error:

mongo -u test -p test

you would have to specify the chat database:

mongo chat -u test -p test

I don't think it is a socket error because you are connecting, the error message seems to suggest that you are just not authenticating on the right database (in this case the chat database)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda