1

I have a node.js application deployed in Azure which connects to Azure CosmosDB (MongoDb database).

To connect to to mongo-db i use this code

    const options = {
        useUnifiedTopology: true,
        useNewUrlParser: true,
    }
    
client = new MongoClient(mongoConnectionUri, options)
            // @ts-ignore
            global._mongoClientPromise = client.connect()
            clientPromise = global._mongoClientPromise

this is working fine when deployed in Azure as containerapp, but when i try to run it from localhost

npm run dev -- -- open

When the part to connect to mongo-db comes, I am getting this error,

MongoServerSelectionError: Server selection timed out after 30000 ms
    at Timeout._onTimeout (xyz/node_modules/mongodb/lib/sdam/topology.js:293:38)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(1) {
      'my-db.mongo.cosmos.azure.com:10255' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'globaldb',
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

MongoDB connection string:

mongodb://my-db:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz==@my-db.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@my-db@

there are no network restrictions

My node version

node --version
v16.17.1

npm version
{
  npm: '8.15.0',
  node: '16.17.1',
  v8: '9.4.146.26-node.22',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '93',
  nghttp2: '1.47.0',
  napi: '8',
  llhttp: '6.0.9',
  openssl: '1.1.1q+quic',
  cldr: '41.0',
  icu: '71.1',
  tz: '2022a',
  unicode: '14.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}

This was working from local before , but suddenly it is not working, From Az cli in my local I am able to retrieve the database details in cosmosdb

az cosmosdb database show --resource-group rgname --name accountname --db-name dbname

Update: If i try the connection string by installing mongosh mongosh and the command

mongosh "mongodb://my-db:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz==@my-db.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@my-db@"
MongoServerSelectionError: Server selection timed out after 30000 ms

I am getting the same errorMongoServerSelectionError: Server selection timed out after 30000 ms

Can anyone please help?

Update: Mobile hotspot: Able to connect with my mobile-hotspot in the same machine (macOS big slur) to the same remote db without any errors.

Tried this will different versions of node.js and mongodb libraries including latest but the same error when connecting with Home wifi. Before recently i had changed the wifi security from wpa to wpa2 personal and ever since this error occurs

Coder
  • 39
  • 6
  • 1
    Please edit to provide more details, such as connection string (please mask the exact instance name and key). Also, are you able to connect via `mongo` command line cli, to verify you have access from your local dev environment? Regarding modifying `package.json` - not exactly sure what that implies - please edit to explain further. – David Makogon Feb 02 '23 at 03:10
  • Thank you, i have tried both azcli and mongosh commands, with mongosh command i am getting the same error – Coder Feb 02 '23 at 14:16

0 Answers0