1) If you can, please include a screenshot of your problem
using pycharm for same code, no problem
`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
`
2) Please include the name of your operating system
Ubuntu 20.04
3) 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
1) If you can, please include a screenshot of your problem![image](https://github.com/getcursor/cursor/assets/8132504/d35696ff-f479-4772-8788-5a0c9d87c533)
using pycharm for same code, no problem
`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 `
2) Please include the name of your operating system Ubuntu 20.04 3) 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