Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda