0
I have the simple routes.ts:
import { Router } from 'express'
import UserController from './controllers/UserController';
const routes = Router();
routes.get('/:User', UserController.index);
export default routes;
And then on my controller UserController:
import { Request, Response } from 'express'
import axios from 'axios';
const api = axios.create({
baseURL: 'api.exemplo.privada'
});
export default {
async index(Req: Request, Res: Response, User: String) {
api
.get('/' + User)
.then((ApiResponse) => {
return Res.send(ApiResponse.data)
})
.catch((err) => {
console.error(err);
});
}
};
When I pass User as parameter in Controller, I get the following error
A última sobrecarga gerou o seguinte erro.
O argumento do tipo '(Req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Res: Response<any, Record<string, any>>, User: String) => Promise<...>' não é atribuível ao parâmetro do tipo 'Application'.
O tipo '(Req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Res: Response<any, Record<string, any>>, User: String) => Promise<...>' não tem as propriedades a seguir do tipo 'Application': init, defaultConfiguration, engine, set e mais 61.ts(2769)"
And this is what I do not understand, because if I pass only the User as parameter, thus, giving a Console, the Controller normally receives it. Everything turns out okay!:
export default {
async index(User: String) {
console.log(User)
}
};
As well, when I only pass the Express Request and Response, with the API giving Get direct (without the User parameter), also wheel ok! (?):
export default {
async index(Req: Request, Res: Response) {
api
.get('/UserUmExemplo')
.then((ApiResponse) => {
return Res.send(ApiResponse.data)
})
.catch((err) => {
console.error(err);
});
}
};
Can someone explain to me why I can’t use the Express+ parameters of the route at the same time?
In the case, (async index(Req: Request, Res: Response, User: String)).
Because the third parameter has to be the type
NextFunction– Cmte Cardeal
So I pass as Null, and User passes as fourth?
– Dasx
No. If you analyze the type, you will see that it does not accept any string. And I could not understand why this User. Why not extract via
req.params.User– Cmte Cardeal
In fact, it makes more sense, but my doubt was pertinent to the same parameters, thank you for explaining friend, I will look about
NextFunction. I parachuted into Typescript today.– Dasx