I am trying to connect mongoDB database with my app but can not be succeeded. I followed another answers related to this but failed that is why I asked here.
This is an old project. It needs to connect and run remaining the old packages versions right now. The project is at a running stage and now needs to connect with mongodb
database
The code example as like below:
package.json
{
"name": "...",
"description": "Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js",
"version": "0.0.1",
"author": "...",
"engines": {
"node": "4.4.4",
"npm": "3.8.x"
},
"scripts": {
"start": "grunt",
"debug": "grunt debug",
"test": "grunt test",
"postinstall": "bower install --config.interactive=false"
},
"dependencies": {
"async": "~0.9.0",
"body-parser": "~1.9.0",
"bower": "~1.3.8",
"chalk": "~0.5",
"compression": "~1.2.0",
"connect-flash": "~0.1.1",
"connect-mongo": "^1.1.0",
"consolidate": "~0.10.0",
"cookie-parser": "~1.3.2",
"express": "^4.13.4",
"express-session": "^1.13.0",
"forever": "~0.11.0",
"glob": "~4.0.5",
"gridfs-stream": "^1.1.1",
"grunt-cli": "~0.1.13",
"helmet": "~0.5.0",
"lodash": "^4.12.0",
"method-override": "~2.3.0",
"mongoose": "4.4.16",
"morgan": "~1.4.1",
"multer": "^1.1.0",
"nodemailer": "~2.4.1",
"passport": "~0.2.0",
"passport-facebook": "~1.0.2",
"passport-github": "~0.1.5",
"passport-google-oauth": "~0.1.5",
"passport-linkedin": "~0.1.3",
"passport-local": "~1.0.0",
"passport-twitter": "~1.0.2",
"paypal-rest-sdk": "^1.6.8",
"q": "^1.5.1",
"stripe": "^4.6.0",
"swig": "~1.4.1",
"validator": "^5.2.0"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-concurrent": "^2.3.0",
"grunt-contrib-csslint": "^1.0.0",
"grunt-contrib-cssmin": "^1.0.1",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-uglify": "^1.0.1",
"grunt-contrib-watch": "^1.0.0",
"grunt-env": "^0.4.4",
"grunt-karma": "^1.0.0",
"grunt-mocha-test": "^0.12.7",
"grunt-ng-annotate": "^2.0.2",
"grunt-nodemon": "^0.4.2",
"karma": "^0.13.22",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.0",
"load-grunt-tasks": "^3.5.0",
"mocha": "^2.4.5",
"should": "^8.3.1",
"supertest": "^1.2.0"
}
}
server.js
var db = mongoose.connect('mongodb://<username>:<password>@cluster0.qlvoh.mongodb.net/<dbname>?retryWrites=true&w=majority', function(err) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
} else {
console.log('Connection Successful');
}
}, {useMongoClient: true});
I used right username, password and db name and tested on mongodb-compass
. It connects in mongodb-compass
successfully. So the username and password are correct indeed.
When I use mongodb srv://...
in the uri
string, the console error is like;
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): app/views/**/*.* gruntfile.js server.js config/**/*.js app/**/*.js
[nodemon] watching extensions: js,html
[nodemon] starting `node server.js`
NODE_ENV is not defined! Using default development environment
Could not connect to MongoDB!
Error: Invalid mongodb uri. Must begin with "mongodb://"
Received: ...
.../node_modules/gridfs-stream/lib/index.js:25
if (!db) throw new Error('missing db argument\nnew Grid(db, mongo)');
^
Error: missing db argument
new Grid(db, mongo)
So I use mongodb://
in connection string and then it returns Could not connect to MongoDB!
as follows:
Noted that the mongoose
version is 4.4.16
.
When I use mongoose
version 5 or higher, it returns an error from node_modules/connect-mongo
as follow:
...node_modules/connect-mongo/src/index.js:105
throw new Error('Connection strategy not found');
^
Error: Connection strategy not found
at new MongoStore...
Anyone help me please to solve this problem?
Thanks in advance!
CodePudding user response:
I am able to connect successfully with only selecting node 2.2.12 or later
option from MongoDB atlas
account.
And the string uri
is as like below:
mongodb://<username>:<password>@cluster0-shard-0000.vnrr0.mongodb.net:27017,cluster0-shard-00-01.vnrr0.mongodb.net:27017,cluster0-shard-00-02.vnrr0.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-134hkh-shard-0&authSource=admin&retryWrites=true&w=majority