Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
.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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!