Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
FastAPI + MongoDB Error: 'id': str(user['_id']), TypeError: string indices must be integers

I am creating a basic login form using oauth2.0 in FastAPI and MongoDB. I am trying to get the email and password from the database but it keeps showing the same error "'id': str(user['_id']), TypeError: string indices must be integers". I am not exactly sure what I am doing wrong.

@authentication.post('/login')
async def login(form_email: OAuth2PasswordRequestForm = Depends(),
                form_password: OAuth2PasswordRequestForm = Depends()):
    email = users_serializer(user_list.find_one({"email": form_email.username}))
    password = users_serializer(user_list.find_one({"password": form_password.password}))
    print(email)
    print(password)
    if form_email.username == email:
        if form_password.password == password:
            return {"status": "ok", "details": f"Welcome! {form_email.username} "}
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Incorrect email or password')

This is the users_serializer schema:

def user_serializer(user) -> dict:
    return {
        'id': str(user['_id']),
        'name': str(user['name']),
        'email': str(user['email']),
        'password': str(user['password']),
    }

Can anyone help me understand what I am doing wrong? Thanks in advance!

PS: I don't have much experience with FastAPI and MongoDB.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!