Handling Mongoose connection failure in NestJS - node.js

I am new to NestJS.
I am trying to connect to mongo instance, like this MongooseModule.forRoot('mongodb://localhost:27017/demo')
How can I handle no connection here.

Probably you wrote wrong database URL.
You have to follow official documentation:
Mongo

Related

server crushes after linking mongo atlas,

Server crushed when i linked mongo atlas on the server.js file,error being generated is connection.once is not a functionserver.js file
I have tried to require once as an eventemitter but with no success
I think the way you have implemented this is wrong. When I am looking at another example i.e Mongoose.connection('once') what does it mean it has applied db.once on a DB where as you are doing it on a connection

MongoDB connection problems

I am trying to connect MongoDB with node.js but whenever I try I am getting this error. (MongoDB is started).
[terminal]JavaScript code
I tried to restart MongoDB.
I found the solution, by replacing the URI from (mongodb://localhost:27017) to
(mongodb://0.0.0.0:27017).
And it worked for me.

I am running an instance of mongodb in docker. How do I get the connection string to use it with RoboT3 or mongoose in my nodejs application?

I am running an instance of mongodb in docker. How do I get the connection string to use it with RoboT3 or mongoose in my nodejs application?
I want to be able to get a connection string like this
mongodb://dbuser:dbpassword#examplehost:63639/name
You need to construct it yourself following the documentation.

Can't authenticate to local MongoDB database

I recently started working on a project with Express and since I'm using Node.js as backend I chose MongoDB as my database. It's my first time working with Mongo but I can't authenticate with Express, it works fine from terminal. I followed the guide from MogoDB blog here. I tried using their Atlas service where I had no problem authenticating. I'm using MongoDB driver. Here's how my connection URI looks like:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?retryWrites=true&w=majority`;
I tried changing mongodb:// to mongodb+srv:// but that resulted in invalid connection string error.
You need to specify the authentication database, usually admin:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?authSource=admin&retryWrites=true&w=majority`;
If you don't specify it then in your case MonogDB defaults the authentication database to cloud - which is most likely wrong.
When you are accessing mongodb on the web you can click on connect and on connect your application. You will show the uri to copy paste starting with
mongodb+srv://USERNAME:PASSWORD#CLUSTER/DATABASE
You forgot to specify the CLUSTER. Currently is your local Database

Should I worry to close db connection while connecting cloudant db from nodejs using cloudant module?

I am connecting to cloudant using "cloudant" module in nodejs (npm install --save cloudant). Below is the code to initiate the db connection variable and then to use it.
//instantiate a cloudant var with the cloudant url. The url has id, password in it.
cloudant = require('cloudant')(dbCredentials.url);
//set the middleware with the db name
db = cloudant.use(dbCredentials.dbName);
//use the db variable to insert data into the db
function(req){
db.insert({
name:req.name,
age:req.age
},id,function(err,doc){
....
});
};
Should I be worrying about closing the connection after I use db variable? It does not make sense to me since we are not using any connection pool here. To me we are simply instantiating the db variable with the endpoint, credentials and db name. Later we are calling the cloudant resources as ReST APIs. I am slightly confused here and dont think we need to do any close connection (which in fact means nothing but nullifying the cloudant variable). Can please share any comments, whether I am wrong or right? Thanks in advance.
By default, the Cloudant library uses the default Node.js connection pooling so it will respect the server's "Keep-Alive" instruction, but is nothing that you need to worry about. Simply keep making Cloudant library function calls and the library will make HTTP connections when required - reusing existing connections when necessary, creating new ones in other cases.

Resources