Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda