node.js - de ce request.query nu mai este 'any'? eroare TypeScript la express request.query
după npm i acesta este eroarea pe care o primesc dacă încerc să transmit parametri de interogare unei funcții care așteaptă un string: Argumentul de tip 'string | Query | (string | Query)[]' nu poate fi atribuit parametrului de tip 'string'.
Tipul 'Query' nu poate fi atribuit tipului '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) {
}
eroarea este în params.shopEventId.. dacă adaug: const params=(req.query as any); funcționează
Răspuns #1
Acest lucru face ca Express să fie mai strict în tipuri. Trebuie să adaugi tipuri.
const shopEventId: string = req.query.shopEventId as string
