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
Related
This question already has answers here:
MongoParseError: options useCreateIndex, useFindAndModify are not supported
(23 answers)
Closed 11 months ago.
While working on backend of whatsapp clone i am facing these errors couldn't understand what type of errors are?
This Project is made using React, Node, Express and Mongodb. I also use pusher.
E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\connection_string.js:289
throw new error_1.MongoParseError(${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported);
^
MongoParseError: options usecreateindex, useunifedtopology are not supported
at parseOptions (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\connection_string.js:289:15)
at new MongoClient (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\mongo_client.js:62:63)
at E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\connection.js:784:16
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\connection.js:781:19)
at E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\index.js:342:10
at E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (E:\Whatapp clone\whatsapp-backend\node_modules\mongoose\lib\index.js:1176:10)
error Command failed with exit code 1.
On your mongoose.connect() you need to omit useCreateIndex and useUnifiedTopology as they are no longer supported.
From the docs:
No More Deprecation Warning Options useNewUrlParser,
useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer
supported options. Mongoose 6 always behaves as if useNewUrlParser,
useUnifiedTopology, and useCreateIndex are true, and useFindAndModify
is false. Please remove these options from your code.
My code is running fine in localhost with no warnings but when I'm changing my connection string to the string provided by mongodb atlas, following errors are showing which you can see in the below pic and my browser keeps circulating plz guide me what I'm doing wrong
(node:13700) DeprecationWarning: 'open()' is deprecated in mongoose >=
4.11.0,
use 'openUri() instead, or set the 'useMongoClient' option if using 'connect()' or 'createConnection()'. See
http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
(Use 'node --trace-deprecation... to show where the warning was
created)
Server started on port 3000
Error [MongoError]: failed to connect to server [undefined:27017] on first connect [Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqwrap.onlookup [as oncomplete] (dns.js:67:26) {}] name: 'MongoError'
at Pool. (C:\Users\Umer\Desktop\node\Alhamdulillah, complete webapp\All Validations Completed\node_modules\mongodb-core\lib\topologies\server.js: 336:35)
at Pool.emit (events.js:315 :20) at Connection.
(C:\Users\Umer\Desktop\node\Alhamdulillah, complet e webapp\A11
Validations Completed\node_modules\mongodb-core\lib\connection\poo1
Lis:280:12)
screenshot
According to the problem that you're facing, it is because you're coding the deprecated method of connecting to the MongoDB.
Your database.js file seems okay but I would suggest that you use dotenv and not a separate file for your secrets
But in your app.js file, kindly completely change the way you're connecting to MongoDB.
const config = require('./config/database')
const options = {useUnifiedTopology: true, useNewUrlParser: true}
mongoose.connect(config.mongoURI, options).then(
()=> console.log('connected to mongodb'),
(reason)=> console.error(`Error: ${reason}`)
)
// All of your other code ahead.
Happy Coding!
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.
So I have this website in Express-Node/MongoDb, managed to install it and have it run smoothly with no errors whatsoever. Then, as you do, I updated 3 miserable files, added a couple of lines of code, nothing fancy (worked locally as well), updated everything on the live server and after the npm start got the error message below (multiple times, everytime I make a call to the db I guess).
MongoDB connection error: MongoError: Topology closed
at NativeTopology.close (/home/jelastic/ROOT/node_modules/mongodb/lib/core/sdam/topology.js:325:38)
at /home/jelastic/ROOT/node_modules/mongodb/lib/mongo_client.js:267:21
at maybePromise (/home/jelastic/ROOT/node_modules/mongodb/lib/utils.js:719:3)
at MongoClient.close (/home/jelastic/ROOT/node_modules/mongodb/lib/mongo_client.js:248:10)
at /home/jelastic/ROOT/node_modules/mongoose/lib/connection.js:711:20
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/home/jelastic/ROOT/node_modules/mongoose/lib/connection.js:709:19)
at Mongoose.connect (/home/jelastic/ROOT/node_modules/mongoose/lib/index.js:335:15)
at Object.<anonymous> (/home/jelastic/ROOT/controllers/visitController.js:7:10)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at Object.<anonymous> (/home/jelastic/ROOT/routes/activity.js:4:24)
I check my connection credentials to the db, everything seems ok... but hey, sometimes you forget a period, a parenthesis, whatever... so I rollback to a previous working version and... got the same error. What's weird too is that the site works just fine as far as I can see. Everything loads fine, I was able to insert new documents to collections, delete some... No error shows up in the console after the the initial display.
I don't get it. What could possibly trigger this error if I ruled out my code?? I don't like it when I get an error that seems blocking yet everything seems to work fine...
Any insight would be much appreciated!!
I configured the connections.js file in config. I used shortcut route to check whether data is getting stored in the database.
I put the url localhost:1337/test/create?name=hello
It creates an entry in the database but server crashes with this log
process.nextTick(function() { throw err; });
^
TypeError: Right-hand side of 'instanceof' is not callable
at _hasBinary (/usr/local/lib/node_modules/sails/node_modules/has-binary/index.js:31:30)
at hasBinary (/usr/local/lib/node_modules/sails/node_modules/has-binary/index.js:58:10)
at Namespace.emit (/usr/local/lib/node_modules/sails/node_modules/socket.io/lib/namespace.js:214:9)
at Object.broadcastToRoom [as broadcast] (/usr/local/lib/node_modules/sails/node_modules/sails-hook-sockets/lib/sails.sockets/broadcast-to-room.js:74:13)
at child.broadcast (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:139:23)
at child.publishCreateSingle (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1218:14)
at /usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1069:16
at arrayEach (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:1439:13)
at Function.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:3500:13)
at child.publishCreate (/usr/local/lib/node_modules/sails/lib/hooks/pubsub/index.js:1068:11)
at child.wrapper [as publishCreate] (/usr/local/lib/node_modules/sails/node_modules/#sailshq/lodash/lib/index.js:3250:19)
at created (/usr/local/lib/node_modules/sails/lib/hooks/blueprints/actions/create.js:48:19)
at wrapper (/usr/local/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
at success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:248:9
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:269:32
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at child.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:152:44)
Update:20-Dec-2017..
This is what i had gotten when in installed the >sails-mongo adapter...What exactly is the version 2.1.20 of..?
Faizaans-MacBook-Pro:foobar faiz$ npm install sails-mongo --save
npm WARN deprecated mongodb#2.1.20: Please upgrade to 2.2.19 or higher
+ sails-mongo#0.12.2
added 14 packages in 7.967s
I couldn't figure out the exact issue here. I tried to connect my project to mysql and the same issue came up. The problem got solved when I created a new project and tried connecting it to the database.