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

181
Visualizações
Ignore undefined values in the find method

I have a service function that should return users based on condition, when it's present, or return just all records from collection:

function getUsers(status: string[]) {
  return this.usersModel.find({ status });
}

But when users don't submit anything in params, the status will be undefined and mongo will search for users with status: undefined. Can I make the mongoose find function ignore status if it's undefined and do not include it in the query?

NOTE: I don't want to explicitly construct the query in the service, as it's violating the service scope of responsibility.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Perhaps I'm missing something here, but is there anything stopping you from simply running a different query (one absent of such conditions) if status is empty?

It is afterall a service function, business logic isn't the end of the world :)

about 4 years ago · Juan Pablo Isaza Relatório
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