Function findById

  • Find a document by id

    Throws

    mongoose error

    Example

    import { findById } from '@mdazad/common-utils';
    import { User } from '../models/user';

    const user = await findById('id', User);

    Example

    if you want to populate fields in response

    import { findById } from '@mdazad/common-utils';
    import { User } from '../models/user';

    const user = await findById('id', User, ['posts']);

    Parameters

    • id: string

      id of the document

    • model: Model<Document<any, any, any>, {}, {}, {}, any>

      mongoose model

    • populateFields: string[] = []

      fields to populate

    Returns Promise<null | Document<any, any, any> & {
        _id: ObjectId;
    }>

    document if found

Generated using TypeDoc