2

I have a mongoDB atlas for application. I have connected to atlas from local env by whitelisting my IP on MongoDB atlas also, I have whitelisted my GCP VPC CIDR and mongo connector as like below enter image description here

I have the following code return for GCP cloud function

const co = require('co');
const mongodb = require('mongodb');

const uri = "mongodb+srv://admin:<PASSWORD>@uat-ca.sozdm.mongodb.net/<DATABASE>?retryWrites=true&w=majority";

exports.helloWorld = (req, res) => {
  co(function*() {
    const client = yield mongodb.MongoClient.connect(uri);

    const docs = yield client.db('automation').collection('test').find().toArray();
    console.log(docs)
    res.send('Result: ' + JSON.stringify(docs));
  }).catch(error => {
    console.log(error.toString())
    res.send('Error: ' + error.toString());
  });
};

I have tested this code on local and it's working fine but whenever I'm trying to run this code on cloud function I'm getting below error even I have added cloud function under same VPC : MongoServerSelectionError: Server selection timed out after 30000 ms at Timeout enter image description here

I have gone through multiple posts like stackoverflow Mongo Community and tried multiple ways but was not been able to solve the problem.

Appreciate your help.

Dipak Dendage
  • 628
  • 1
  • 6
  • 25
  • Does this answer your question? [MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms](https://stackoverflow.com/questions/59162342/mongodb-connection-error-mongotimeouterror-server-selection-timed-out-after-30) – Sathi Aiswarya Mar 16 '22 at 09:33
  • @SathiAiswarya Tried but does not help to solve the issue – Dipak Dendage Mar 16 '22 at 10:52

0 Answers0