Búsqueda de sitios web

node.js - ¿por qué request.query ya no es 'any'? error de TypeScript en express request.query


después de npm i este es el error que obtengo si intento pasar parámetros de consulta a una función que espera una cadena: Argument of type 'string | Query | (string | Query)[]' is not assignable to parameter of type 'string'.\n 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) {
}

el error está en params.shopEventId.. si agrego: const params=(req.query as any); funciona

Respuesta #1

Esto hace que express sea más estricto en los tipos. Tienes que agregar tipos.

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

Artículos relacionados