Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Failed to connect error with mongodb atlas

Hello!
Im making a discord bot. I’m using node.js and mongoose.js and I have it running locally in a docker container and everything works fine. I’m on an M2 cluster. Deploying it to google cloud run using artifact registry. I have set up a VPC connector, router and NAT, with a reserved external IP, which is whitelisted in atlas. I have set up secrets api, and attached the secrets along with the VPC to the cloud run service. I can see that that bot is connected to discord via the logs and discord itself.

my issue is that I get this error connecting to atlas: “Failed to connect to MongoDB: MongoParseError: Invalid scheme, expected connection string to start with “mongodb://” or “mongodb+srv://””

uri: mongodb+srv://:@guildinfo.enxyktu.mongodb.net/?retryWrites=true&w=majority where username and password are correctly changed, neither have any special characters that need escaping, nor does it end in a ;

I can run the same code locally, only when it’s deployed on cloud run is there an issue. I barely know what I’m doing, It’s taken about 5 days to get from container to this error.

from the cloud run yaml:
```

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: servicename
  namespace: 'somenumbers'
  selfLink: >-
    /apis/serving.knative.dev/v1/blahblah
  uid: stuff
  resourceVersion: AAYKYG6p9Ss
  generation: 5
  creationTimestamp: '2023-11-17T20:04:29.183296Z'
  labels:
    cloud.googleapis.com/location: us-central1
  annotations:
    run.googleapis.com/client-name: cloud-console
    serving.knative.dev/creator: email@gmail.com
    serving.knative.dev/lastModifier: email@gmail.com
    run.googleapis.com/operation-id: numbers
    run.googleapis.com/ingress: all
    run.googleapis.com/ingress-status: all
spec:
  template:
    metadata:
      labels:
        client.knative.dev/nonce: prefilledblahblah
        run.googleapis.com/startupProbeType: Default
      annotations:
        run.googleapis.com/client-name: cloud-console
        autoscaling.knative.dev/minScale: '1'
        run.googleapis.com/vpc-access-egress: all-traffic
        autoscaling.knative.dev/maxScale: '100'
        run.googleapis.com/vpc-access-connector: >-
          projects/myprojectname/locations/us-central1/connectors/externalblahblah
        run.googleapis.com/startup-cpu-boost: 'false'
    spec:
      containerConcurrency: 80
      timeoutSeconds: 300
      serviceAccountName: blahblah@blahblah.gserviceaccount.com
      containers:
      - name: name-1
        image: >-
          image/from/artifactregistry
        ports:
        - name: http1
          containerPort: 8080
        env:
        - name: clientId
          valueFrom:
            secretKeyRef:
              key: latest
              name: clientId
        - name: guildId
          valueFrom:
            secretKeyRef:
              key: latest
              name: guildId
        - name: mongoToken
          valueFrom:
            secretKeyRef:
              key: latest
              name: mongoToken
        - name: TOKEN
          valueFrom:
            secretKeyRef:
              key: latest
              name: TOKEN
        - name: EXTPORT
          valueFrom:
            secretKeyRef:
              key: latest
              name: PORT
        resources:
          limits:
            cpu: 1000m
            memory: 512Mi
        startupProbe:
          timeoutSeconds: 240
          periodSeconds: 240
          failureThreshold: 1
          tcpSocket:
            port: 8080
     
  traffic:
  - percent: 100
    latestRevision: true
```
my code that handles the connections:
```
const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Discord bot is alive!');
});
codeFlow('r', `mongoToken : ${mongoToken}`);
const PORT = process.env.EXTPORT || 3000;
server.listen(PORT, () => {
    console.log(`HTTP server listening on port ${PORT}`);
});
(async () => {
    try {
        await mongoose.connect(mongoToken);
        console.log('Connected to MongoDB');
    } catch (error) {
        console.error('Failed to connect to MongoDB:', error);
    }
})();
```
Any help would be really appreciated



0 0 658
0 REPLIES 0