'.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 {};
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source