Function findByIdAndSendResponse

  • Find a document by id and send response

    Throws

    mongoose error

    Example

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

    const router = express.Router();

    router.get("/:id", async (req, res) => {
    await findByIdAndSendResponse(req, res, User);
    });

    Example

    if you want to populate fields in response

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

    const router = express.Router();

    router.get("/:id", async (req, res) => {
    await findByIdAndSendResponse(req, res, User, ['posts']);
    });

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      express request

    • res: Response<any, Record<string, any>>

      express response

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

      mongoose model

    • Optional populateFields: string[]

      fields to populate

    Returns Promise<void>

    document in response if found

Generated using TypeDoc