3
votes

i downloaded mongoDB and it works just fine but when i want to use REST API and use npm run dev in command line it gave me this error.. That's an error

(node:10100) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (D:\Node Js\task-manager\node_modules\mongoose\lib\connection.js:800:32)
    at D:\Node Js\task-manager\node_modules\mongoose\lib\index.js:341:10
    at D:\Node Js\task-manager\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (D:\Node Js\task-manager\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
    at Mongoose.connect (D:\Node Js\task-manager\node_modules\mongoose\lib\index.js:340:10)
    at Object.<anonymous> (D:\Node Js\task-manager\src\db\mongoose.js:3:10)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (D:\Node Js\task-manager\src\index.js:2:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
(node:10100) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10100) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

and before that when i wanted to connect database and project it work but now its not work and when i try to connect it is stopping And thats not connecting picture

7

7 Answers

3
votes

Take in account that MongoDB should be running before issuing npm run dev.

You have to initialize database engine with mongod command first. Sometimes it's easy to forget this step.

1
votes

I think your URL is not properly formatted,

use below URL

mongodb://localhost:27017/{DATABASE_NAME}
0
votes

At first start your MongoDB on your local machine and connect it using localhost:27017. Also don't forget to use URL mongodb://localhost:27017/{DATABASE_NAME} in your code.

0
votes

I think you need to install and run the MongoDB server on your machine.

Then try the below code in your program

mongoose.connect('mongodb://localhost/myapp', { useNewUrlParser: true });

To check MongoDB service, you can follow in Task Manager service or running "mongo" command in Command Prompt.

mongo_command

0
votes

I am using a tutorial on using the mongoDb, and I came accross this error. I am using Windows environments, so i just reinstalled my mongo community server. before i was just having the mongoDb compass without any server installed that would listening. It works.

0
votes

Posting for future searchers, i was using docker-compose and i got this error. I was passing the user and the password by environment variables, and forgot to replicate it into docker-compose, so my variables were returning undefined.

If you got this error, you should verify if you're passing the variables into your .yml file to nodejs container.

i.e.:

node:
    image: "node:16.3.0"
    user: "node"
    working_dir: /home/node/app
    environment:
      - NODE_ENV=${NODE_ENV}
      - PORT=${PORT}
      - MONGO_USERNAME=${MONGO_USERNAME}
      - MONGO_PASSWORD=${MONGO_PASSWORD}
-1
votes

For Windows, Just repair from MongoDBCommunity Edition MSI Installer and connect via Compass or your Node JS application. Files might have gone corrupt.