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.
Related
I need to connect to my DB. When I hit npm start in my cli, this error pops up...
I've looked online for similar issues but people have one line of code similar to this one:
mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}#db:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});
In my case, I only have an .env file with my logs. I've tried to put this inside my server.js file but it didn't work.
here's the error :
MongoDB connection error: MongoParseError: Invalid connection string
at parseConnectionString (/home/mathieu/projects/techproject/shopster/node_modules/mongodb/lib/core/uri_parser.js:565:21)
at connect (/home/mathieu/projects/techproject/shopster/node_modules/mongodb/lib/operations/connect.js:282:3)
at /home/mathieu/projects/techproject/shopster/node_modules/mongodb/lib/mongo_client.js:223:5
at maybePromise (/home/mathieu/projects/techproject/shopster/node_modules/mongodb/lib/utils.js:662:3)
at MongoClient.connect (/home/mathieu/projects/techproject/shopster/node_modules/mongodb/lib/mongo_client.js:219:10)
at /home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/connection.js:791:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/connection.js:788:19)
at /home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/index.js:342:10
at /home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
at Mongoose.connect (/home/mathieu/projects/techproject/shopster/node_modules/mongoose/lib/index.js:341:10)
at Object.<anonymous> (/home/mathieu/projects/techproject/shopster/models/ProductModel.js:4:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10) {
name: 'MongoParseError'
}
Can you please check which version of Mongoose you are using?
If you're using version v.6 or above, you no longer needed to specify useNewUrlParser: true.
Mongoose 6 always behaves as if it's true, therefore this option is not required. Once you remove that option, your code should work fine.
References:
Migrating to 6.x
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
Summary: Node/Express API on Cloud Run not connecting to Mongo cluster on MongoDB Atlas
Our Express/Node API is being deployed to Google's new Cloud Run for container deployments. The API, when run locally with docker using docker run -p 8080:8080 <image_id>, fetches data from our Mongo database just fine. The relevant logs from within Cloud Run:
2020-04-22T18:15:15.408778Z(node:1) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection timed out
2020-04-22T18:15:15.408824Z at new MongooseServerSelectionError (/usr/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
2020-04-22T18:15:15.408834Z at NativeConnection.Connection.openUri (/usr/app/node_modules/mongoose/lib/connection.js:823:32)
2020-04-22T18:15:15.408844Z at Mongoose.connect (/usr/app/node_modules/mongoose/lib/index.js:333:15)
2020-04-22T18:15:15.408854Z at Object.<anonymous> (/usr/app/models/index.js:11:10)
2020-04-22T18:15:15.408862Z at Module._compile (internal/modules/cjs/loader.js:1123:30)
2020-04-22T18:15:15.408871Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
2020-04-22T18:15:15.408880Z at Module.load (internal/modules/cjs/loader.js:972:32)
2020-04-22T18:15:15.408893Z at Function.Module._load (internal/modules/cjs/loader.js:872:14)
2020-04-22T18:15:15.408902Z at Module.require (internal/modules/cjs/loader.js:1012:19)
2020-04-22T18:15:15.408911Z at require (internal/modules/cjs/helpers.js:72:18)
2020-04-22T18:15:15.408919Z at Object.<anonymous> (/usr/app/config/passport.js:7:12)
2020-04-22T18:15:15.408927Z at Module._compile (internal/modules/cjs/loader.js:1123:30)
2020-04-22T18:15:15.408937Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
2020-04-22T18:15:15.408946Z at Module.load (internal/modules/cjs/loader.js:972:32)
2020-04-22T18:15:15.408954Z at Function.Module._load (internal/modules/cjs/loader.js:872:14)
2020-04-22T18:15:15.408964Z at Module.require (internal/modules/cjs/loader.js:1012:19)
2020-04-22T18:15:15.608498Z(node:1) 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)
2020-04-22T18:15:15.608686Z(node:1) [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.
...
2020-04-22T18:19:59.772303ZGET504296 B300.5 sChrome 81 https://node-api-deploy-q62r35344q-uw.a.run.app/api/cbb/home-page-stats
The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout
2020-04-22T18:20:26.472145ZGET504290 B300 sChrome 81 https://node-api-deploy-q62r35344q-uw.a.run.app/api/cbb/teams/list
The request has been terminated because it has reached the maximum request timeout. To change this limit, see https://cloud.google.com/run/docs/configuring/request-timeout
This makes it seem clear that the data is not being fetched, and the requests are then timing out. My Mongo database is hosted in MongoDB Atlas, and I haven't done anything in particular (whitelist the IP of the cloud run server, etc.) that I might have to do to make this work. Does cloud run even have an IP, or no because it is serverless. Maybe I have to pass the DB URI to cloud run through an env variable? I'm not sure.
Edit
Here's a relevant Stack Overflow post that also mostly went unanswered.
Cloud Run now has support for configuring VPC egress settings that lets you define a static IP for outbound requests through Cloud NAT. You can follow this step by step guide in the documentation to configure a static IP to whitelist at MongoDB Atlas.
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
I have been following along to Acadamind's NodeJS / Express / MongoDb-build a shopping cart tutorial on youtube and everything was going fine until I restarted the mongodb server when I wanted to update a product item.
Before this change, I would write npm start and the code would run smoothly, but now I am given a response of:
(node:49784) UnhandledPromiseRejectionWarning: Error: connect
ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
(node:49784) 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:49784) [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.
I am new to node.js and am not sure why this is occuring when I believe all i did was change a product item in my seed folder.
Here is the link to my github repository:clone my git hub "shopping-cart" repository here
Note: since this is my first node.js project that I am having problems with, is this the correct format to ask for help due to the larger nature or the project.
Thanks for any feedback!!!
Mongod needs to be running before starting the app:
sudo service mongod start
Status check:
sudo service mongod status
(should show Active (running))