MongoDB replica-set starts on DESKTOP instead of localhost - node.js

I want to use transactions with mongoose, therefore I installed run-rs to test it in the development stage. According to guides like this one https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 run-rs starts it on the localhost. When I run run-rs -v 4.2.5 --keep --shell to start the replica-set via the terminal it says: Started replica set on "mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs". Why is it saying DESKTOP for me instead of localhost and how would I connect to it in mongoose.connect()?
I tried the following in my index.js:
const app = express();
const PORT = 27017;
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
mongoose.Promise = global.Promise;
mongoose.connect(dbUri, {
replicaSet: 'rs',
useNewUrlParser: true,
useUnifiedTopology: true
});
Also how would I append the databasename?
UPDATE:
PS C:\Users\Workstation\project> npm start
> project#1.0.0 start C:\Users\Workstation\project
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Your server is running on Port 4000
(node:7436) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
at new MongooseServerSelectionError (C:\Users\Workstation\project\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Workstation\ project\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\Workstation\ project\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:/Users/Workstation/project/index.js:22:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at loader (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at Object.<anonymous> (C:\Users\Workstation\project\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
(node:7436) 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(). (rejection id: 1)
(node:7436) [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.
UPDATE
I accepted the answer below, because the string for connecting works, hence the original question is solved. However I switched to Linux and it works now without further setup. run-rs didn't work on my Windows machine it seems.

replace:
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
with:
const dbUri = 'mongodb://localhost:27017,localhost,localhost:27019/databasenaeme'
Also need to run mongoDb at these port manully. use link for refrence:
link

Related

Node js : Mongoose and mongo connecting problem

While I try to connect mongodb with express js with mongoose and I am getting error for a long. How to fix this is ?
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from node js')
})
const connection_url = 'connection-url';
mongoose.connect(connection_url, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(()=> console.log("Database Connected"));
app.listen(3000);
This is the error
node:14460) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\connection.js:846:32)
at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:351:10
at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:1149:10)
at Mongoose.connect (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:350:20)
at Object.<anonymous> (C:\MyFolder\Coding\DB connection\index.js:11:10)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14460) 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:14460) [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.
How to fix this is and connect to the mongodb ??
Go to MongoDB Atlas
Open your cluster
Go to settings > Network Access
Add IP Addresss
Click on Allow access on anywhere || Just add your current IP
Restart your app and it should be working

Docker authentication failed when connecting to MongoDb

When I try to run a Docker container and connect to a local MongoDB database, it gives me this error:
UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Authentication failed.
this is the connection url in the end: mongodb://pody-admin:Nt4VzWPtDhkCxESjXVJx#localhost:27017/podify
I am trying to get an Auth system that someone else made to work on my end(cloned the repo).
I really don't have experience with working on team projects and using other people's code, let alone Docker.
I'm not really expecting a solution, but would like to know where to look to even understand what might be wrong.
This is what I get in the cmd:
$ npm run up
server#1.0.0 up D:\projects\SOS\podify\Server
docker-compose up -d
Starting server_db_1 ... done
Starting server_cache_1 ... done
server#1.0.0 postup D:\projects\SOS\podify\Server
npm run dev
server#1.0.0 dev D:\projects\SOS\podify\Server
npm run dev --prefix api
api#1.0.0 dev D:\projects\SOS\podify\Server\api
nodemon --exec ts-node src/index.ts
[nodemon] 2.0.4
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): .
[nodemon] watching extensions: ts,json
[nodemon] starting ts-node src/index.ts
Connection url => mongodb://pody-admin:Nt4VzWPtDhkCxESjXVJx#localhost:27017/podify
(node:5072) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Authentication failed.
at NativeConnection.Connection.openUri (D:\projects\SOS\podify\Server\api\node_modules\mongoose\lib\connection.js:828:32)
at Mongoose.connect (D:\projects\SOS\podify\Server\api\node_modules\mongoose\lib\index.js:335:15)
at D:\projects\SOS\podify\Server\api\src\index.ts:10:20
at Object. (D:\projects\SOS\podify\Server\api\src\index.ts:21:3)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Module.m._compile (D:\projects\SOS\podify\Server\api\node_modules\ts-node\src\index.ts:858:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.require.extensions. [as .ts] (D:\projects\SOS\podify\Server\api\node_modules\ts-node\src\index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at main (D:\projects\SOS\podify\Server\api\node_modules\ts-node\src\bin.ts:227:14)
at Object. (D:\projects\SOS\podify\Server\api\node_modules\ts-node\src\bin.ts:513:3)
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)
(node:5072) 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:5072) [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.
[nodemon] clean exit - waiting for changes before restart

UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

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
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.
I think your URL is not properly formatted,
use below URL
mongodb://localhost:27017/{DATABASE_NAME}
In my case, when I changed "mongodb://localhost:27017" to "mongodb://127.0.0.1:27017", it worked
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.
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.
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.
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}
Please check whether mongod is running on default port of 27017.
Run mongod locally before running the mongoose code
For setting up mongo and mongod default on bash shell on windows follow given steps :
cd ~
touch .bash_profile
vim .bash_profile
Go into insert mode on vim and edit the file as follows
alias mongod="/c/Program\ Files/mongodb-5.0.5/bin/mongod.exe"
alias mongo="/c/Program\ Files/mongodb-5.0.5/bin/mongo.exe"
Enter esc and type :wq and press enter
Now you can access mongo and mongod on shell using their names
For Windows, Just repair from MongoDBCommunity Edition MSI Installer and connect via Compass or your Node JS application. Files might have gone corrupt.

EADDRINUSE gets generated on every save with Nodemon

I use Ubuntu 20.04, Nodemon 2.0.4, Node 14.9.0 and Express 4.17.1. Every time I save my code I get this error:
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (/home/ubuntu/kopum/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/ubuntu/kopum/app.js:37:5)
at Module._compile (internal/modules/cjs/loader.js:1075:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
And then I try to run this on terminal:
sudo lsof -i :3000
kill -9 {PID}
Then I restart the Nodemon by running nodemon app.js and everything's back to normal. But the EADDRINUSE is back again when I add some code and save it.
A simple fix that worked when I had a similar issue is to delay Nodemon's restart slightly:
nodemon --delay 500ms app.js
This seems to give a little time for ports to be correctly released.
Seems like nodemon does not correctly kill child-spawn processes before start;
Edit the scripts attribute in your package.json file and add a kill command to terminate any processes listening on your port before the server starts.
"scripts": {
"start": "npm run kill && nodemon app.js",
"kill": "kill -9 $(lsof -i :3000) &>/dev/null | exit 0"
}
try running npx kill-port 3000 after every save command

How to solve running node server error

I am keep facing the below error when running the node server, but the code used to work properly before. Could any expert to resolve the issue?
Below is the error part of server.js
try {
app.listen(port, function(){
console.log('Server started on port'+port);
})
} catch (e) {
console.log('There was an error', e);
}
-------------------------------error---------------------------------------
PS C:\Users\User\WebstormProjects\myapp> nodemon server
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server app.js`
events.js:182
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at Server.setupListenHandle [as _listen2] (net.js:1319:14)
at listenInCluster (net.js:1367:12)
at Server.listen (net.js:1467:7)
at Function.listen (C:\Users\User\WebstormProjects\myapp\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\User\WebstormProjects\myapp\server.js:52:9)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
[nodemon] app crashed - waiting for file changes before starting...
Another app is already using port 3000. You can start it with another port.
You app is working, maybe you just started it twice, you can try skill node & start it again, or change your variable port by 3001

Resources