cursor icon indicating copy to clipboard operation
cursor copied to clipboard

Connection to MongoDB Not working

Open mikahoy045 opened this issue 1 year ago • 0 comments

  1. If you can, please include a screenshot of your problem image

using pycharm for same code, no problem

image

`from fastapi import HTTPException from motor.motor_asyncio import AsyncIOMotorClient from starlette.status import HTTP_503_SERVICE_UNAVAILABLE from dotenv import load_dotenv import os

load_dotenv() # Load environment variables

MONGO_URI = os.getenv("MONGO_URI") # Make sure this matches your .env variable name MONGO_DB = os.getenv("MONGO_DB") client = None

def get_database(): global client if client is None: # Only create a client if it doesn't already exist try: client = AsyncIOMotorClient(MONGO_URI) except Exception as e: raise HTTPException(status_code=HTTP_503_SERVICE_UNAVAILABLE, detail="Could not connect to the database") from e return client[MONGO_DB] # Replace 'mydatabase' with your actual database name `

  1. Please include the name of your operating system Ubuntu 20.04
  2. If you can, steps to reproduce are super helpful Nothing, just code and run it, this code should connect to mongodb , but it's not. Testing with same code (access same repository) from another IDE it works, no problem. And Yes, I have setup the environment also

mikahoy045 avatar May 14 '24 17:05 mikahoy045