MongoDB Atlas error in connection

const MongoClient = require(‘mongodb’).MongoClient;

const url =
‘mongodb+srv://shivam20214008:password@cluster0.haz3nqy.mongodb.net/products_test?retryWrites=true&w=majority’;

const client = new MongoClient(url,{useNewUrlParser: true,useUnifiedTopology: true});
const createProduct = async (req, res, next) => {
const newProduct = {
name: req.body.name,
price: req.body.price
};

console.log(“Going to try.”);

try {
console.log(“Going to try.”);
const a = await client.connect(
process.env.MONGO_URL,
options,
(err) =>{
if(err) console.log(err)
else console.log(“mogodb is connected!”);
}
);
console.log(a);
console.log(“Going to try.”);
const db = client.db();
console.log(“Going to try.”);
const result = db.collection(‘products’).insertOne(newProduct);
console.log(“Going to try.”);
//console.log(result);
} catch (error) {
// console.log(error);
return res.json({message: ‘Could not store data.’});
};
client.close();

res.json(newProduct);
};

const getProducts = async (req, res, next) => {
const client = new MongoClient(url);

let products;
try{

await client.connect();
const db = client.db();

products = await db.collection('products').find().toArray();

}catch(error){
return res.json({message: “Could not retrieve products”});
};

client.close();
res.json(products);

};

exports.createProduct = createProduct;
exports.getProducts = getProducts;

It is giving error I have spent all the day searching for the error but i couldn’t find one.
The code inside the try block is executing only till the “Going to try” block after that block another “Going to try” block is there but it is not working.
If anyone can help?
Thanks in advance.

Hi @Shivam_Kumar13,

It would be helpful to understand what the actual error message is. If you can provide this, then we may be able to assist easier.

I’ve also sent you a DM regarding the connection string.

Regards,
Jason