0

I'm having trouble trying to connect mongodb's localhost to Node. I've tried all kinds of solutions and none of them solve the problem, I honestly don't know what to do anymore. Sorry if I asked the question wrong, I'm new. Attached the error that the console throws me:

enter image description here

I attach my code:


        var mongoose = require('mongoose');

        mongoose.set('useFindAndModify', false);

        mongoose.Promise = global.Promise;

        mongoose.connect('mongodb://localhost:27017/portafolio', {useNewUrlParser:true, 
        useUnifiedTopology: true})
        .then(() => {
        console.log('Conexion a la base de datos establecida');
         });
        .catch(err => console.log(err)); 
hoangdv
  • 15,138
  • 4
  • 27
  • 48
RRR
  • 21
  • 5
  • 1
    is mongodb running and the database you are trying to access created when you run your script? – thebigfundamentals Jun 29 '21 at 00:18
  • Yes, mongodb is running and the database its already created – RRR Jun 29 '21 at 00:20
  • Can you please provide info on how this is running? Also, have you tried commenting out `useUnifiedTopology`? calling it just like `mongoose.connect('mongodb://localhost:27017/portafolio', {useNewUrlParser: true});`. Not sure if it would solve the problem, but I've read something about it. Edit: may be... https://stackoverflow.com/questions/20386464/econnrefused-error-when-connecting-to-mongodb-from-node-js – thebigfundamentals Jun 29 '21 at 00:27
  • 2
    ECONNREFUSED means the operating system active rejected the connection because there was not process listening on the requested port – Joe Jun 29 '21 at 00:49
  • Thanks for the contribution, I tried the solution you told me, it didn't work, I also read the post you shared, I didn't find a solution either ... – RRR Jun 29 '21 at 01:10
  • Provide the way you confirm that the mongo service is running. – hoangdv Jun 29 '21 at 01:28
  • I did not understand, what do you mean? – RRR Jun 29 '21 at 01:30
  • I have mongod open, the service is running (check it in services, inside "system tools") – RRR Jun 29 '21 at 01:32
  • are you running mongoDB from the terminal shell by calling `mongo`, then setting the database to `portafolio` by `use portafolio`? – thebigfundamentals Jun 29 '21 at 02:05
  • This is what I do: - Manually start "mongod" in the file folder of mongodb -Then I start Robo 3T and start the localhost server. - Then I run "npm start" in the terminal – RRR Jun 29 '21 at 02:37
  • Ok. I suggest to you to install MongoDB Community Edition https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ , then run mongo in your terminal by simply calling `mongo`, and it will give you something like this: `connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb`. call `use portafolio` on the mongo terminal to set to portafolio database. Start your script in a new terminal, then you are probably going to be able to connect to it with mongoose. – thebigfundamentals Jun 29 '21 at 03:02
  • Please don't paste screenshots, use formatted text. See https://meta.stackoverflow.com/a/285557/3027266 – Wernfried Domscheit Jun 29 '21 at 07:49
  • Is your app listening as below? How are you running your app (node app.js or similar command)? app.listen(8000, function () { console.log('Example app listening on port 8000!') }) – jamesonrunnels Jun 29 '21 at 02:35

1 Answers1

1

This is the minimum needed to connect the myapp database running locally on the default port (27017). For local MongoDB databases, we recommend using 127.0.0.1 instead of localhost. That is because Node.js 18 and up prefer IPv6 addresses, which means, on many machines, Node.js will resolve localhost to the IPv6 address ::1 and Mongoose will be unable to connect, unless the mongodb instance is running with ipv6 enabled.