Erro ao conectar no mongodb | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Markley

4 meses atrás Markley

Erro ao conectar no mongodb

Estou com problema ao conectar no mongodb, quando eu rodo a aplicação com docker-compose up, retorna o seguinte erro: ``` node_1 | Mongoose error => { MongoError: failed to connect to server [mongo:27017] on first connect [MongoError: connect ECONNREFUSED 172.18.0.3:27017] node_1 | at Pool.<anonymous> (/home/node/app/node_modules/mongodb-core/lib/topologies/server.js:336:35) node_1 | at Pool.emit (events.js:198:13) node_1 | at Connection.<anonymous> (/home/node/app/node_modules/mongodb-core/lib/connection/pool.js:280:12) node_1 | at Object.onceWrapper (events.js:286:20) node_1 | at Connection.emit (events.js:198:13) node_1 | at Socket.<anonymous> (/home/node/app/node_modules/mongodb-core/lib/connection/connection.js:189:49) node_1 | at Object.onceWrapper (events.js:286:20) node_1 | at Socket.emit (events.js:198:13) node_1 | at emitErrorNT (internal/streams/destroy.js:91:8) node_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) node_1 | at process._tickCallback (internal/process/next_tick.js:63:19) node_1 | name: 'MongoError', node_1 | message: node_1 | 'failed to connect to server [mongo:27017] on first connect [MongoError: connect ECONNREFUSED 172.18.0.3:27017]' } ``` Já dei uma pesquisada e não achei nada parecido para resolver meu erro. Aqui esta meu arquivo de connection.js: ``` let mongoose = require('mongoose'); mongoose.connect('mongodb://mongo:27017/email_marketing', { useMongoClient: true }, function (err) { if (err) { console.log('Mongoose error =>', err); } console.log('Mongoose connected'); }); module.exports = mongoose; ``` É aqui está meu docker-composer.yml ``` version: "2" services: mongo: image: "mongo:4.2.2" volumes: - /data/mongodb/db:/data/db ports: - "27017:27017" node: image: "node:10.18.0" user: "node" working_dir: /home/node/app environment: - NODE_ENV=development volumes: - ./:/home/node/app ports: - "3000:3000" command: "node_modules/.bin/nodemon -L --exec npm start" ``` Também meu arquivo de package.json ``` { "name": "sistema-email-nodejs-vuejs", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", "express": "~4.16.1", "http-errors": "~1.6.3", "jwt-simple": "^0.5.6", "mongoose": "^4.13.20", "morgan": "~1.9.1", "nodemon": "^2.0.2", "passport": "^0.4.1", "passport-jwt": "^4.0.0" } } ```

2 Respostas