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

155
Visualizações
.findOne / .findById keep running if no result is found

I face an issue that i can't resolve alone.

I have a MongoDB collection, in this collection i have 1 document atm.

When i use .findById(_id) or .findOne({_id : id}) with the right _id, everything works.

When i use .findById(_id) or .findOne({_id : id}) with the wrong _id (for test purposes), i have no response (no undefined, no null, nothing) from the DB and my request keep running.

Ty for your time, take care !

EDIT :

  • Document :
export interface OrderDocument extends mongoose.Document {
  user: UserDocument['_id'];
  asset_bought: AssetDocument['_id'];
  asset_b_symbol: string;
  asset_sold: AssetDocument['_id'];
  asset_s_symbol: string;
  exchange: ExchangeDocument['_id'];
  exchange_name: string;
  is_draft: Boolean;
  amount: number;
  atm_price: number;
  date: Date;
}
  • Collection's schema :
const orderSchema = new mongoose.Schema(
  {
    _id: { type: mongoose.Schema.Types.ObjectId },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'users',
      required: true,
    },
    asset_bought: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'assets',
      required: true,
    },
    asset_b_symbol: {
      type: String,
      required: true,
    },
    asset_sold: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'assets',
      required: true,
    },
    asset_s_symbol: {
      type: String,
      required: true,
    },
    exchange: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'exchanges',
      required: true,
    },
    exchange_name: {
      type: String,
      required: true,
    },
    is_draft: { type: Boolean, default: false },
    amount: { type: Number, required: true },
    atm_price: { type: Number, required: true },
    date: { type: Date, required: true },
  },
  { timestamps: true }
);
  • Service
export async function findAndPopulateOrders(
  searchType: 'id' | 'one' | 'many',
  query: FilterQuery<OrderDocument>,
  _collections: Array<string>
) {
  const collections = _collections.join(' ');

  if (searchType === 'id') {
    return await OrderModel.findById(query).populate(collections);
  } else if (searchType === 'one') {
    return await OrderModel.findOne(query).populate(collections);
  } else if (searchType === 'many') {
    return await OrderModel.find(query).populate(collections);
  } else {
    return {};
  }
}
about 4 years ago · Juan Pablo Isaza
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