dang
dang

Reputation: 1588

why request.query is not 'any' anymore? express request.query typescript error

after npm i this is the error that i get if i try to pass query params to a function that expects string: Argument of type 'string | Query | (string | Query)[]' is not assignable to parameter of type 'string'.
Type 'Query' is not assignable to type 'string'.ts(2345)

import express from "express";
async function getProductsImagesByShopEvent(req: express.Request, res: express.Response, 
next: express.NextFunction) {
  try {
    const params = req.query;
    if (!params || !params.shopEventId)
        throw new CustomError("params are missing in /business/getProductsImagesByShopEvent", 400, "params are missing");

    const shopEvent = new ShopEvent();
    const events = await shopEvent.getProductsImagesByShopEvent(params.shopEventId);
    res.json(events);
  }
  catch (error) {
    next(error);
  }
}

async getProductsImagesByShopEvent(shopEventId: string) {
}

the error is in params.shopEventId.. if i add: const params = (req.query as any); it works

Upvotes: 8

Views: 14256

Answers (1)

marcellsimon
marcellsimon

Reputation: 706

This makes express more strict in typings. You have to add types.

const shopEventId: string = req.query.shopEventId as string

Upvotes: 7

Related Questions