I would add types to my NestJS server.
I have written a controller (route for Express lovers) then tried to specify the type for params:
public async getAllMessages(
@Query('startDate', ValidateDate) startDate: string,
@Query('endDate', ValidateDate) endDate: string,
@Res() res: Response,
): Promise<string> {
const data = await this.crudService.getPeriodicMessages(startDate, endDate);
return res.status(HttpStatus.OK).send(data);
}
Here I've added the type Response
to res
, since I get this error message in res.status(HttpStatus.OK).send(data)
.
ERROR : Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.
.
I have checked the Response type has status
member of type number
and HttpStatus.OK
is of type enum number
also, so how can I convert it to number
?
I have also read other threads but couldn't solve my problem.
In this example there is no need to inject the response object with @Res()
.
You can just do the following, nest will handle the response automatically. The http status code will always be 200 by default (201 for POST).
public async getAllMessages(@Query('startDate', ValidateDate) startDate: string,
@Query('endDate', ValidateDate) endDate: string): Promise<string> {
return this.crudService.getPeriodicMessages(startDate, endDate);
}
You only need to inject the response object for special cases, like setting the response code dynamically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.