英文:
Property 'cookies' does not exist on type 'Request'
问题
以下是您要翻译的内容:
I'm trying to read cookie in a nestjs controller.
i am following the docs at https://docs.nestjs.com/techniques/cookies#use-with-express-default
Here's my code
import { Controller, Get, Render, Req } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
@Render('home')
getHello(@Req() req: Request) {
return { text: req.cookies['id'] };
}
}
the problem is that type Request
from express does not have cookies
. So i get this error.
src/app.controller.ts:11:24 - error TS2339: Property 'cookies' does not exist on type 'Request'.
11 return { text: req.cookies['id'] };
~~~~~~~
The code actually works if i remove type Request
from req
. But thhen i lost type-safety.
英文:
I'm trying to read cookie in a nestjs controller.
i am following the docs at https://docs.nestjs.com/techniques/cookies#use-with-express-default
Here's my code
import { Controller, Get, Render, Req } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
@Render('home')
getHello(@Req() req: Request) {
return { text: req.cookies['id'] };
}
}
the problem is that type Request
from express does not have cookies
. So i get this error.
src/app.controller.ts:11:24 - error TS2339: Property 'cookies' does not exist on type 'Request'.
11 return { text: req.cookies['id'] };
~~~~~~~
The code actually works if i remove type Request
from req
. But thhen i lost type-safety.
答案1
得分: 4
你需要从express
中导入Request
类型(因此安装@types/express
)。你目前使用的那个不是它。我假设你是默认的http适配器。
英文:
you need to import the type Request
from express
(so install @types/express
). That one you're using is not from it. I'm assuming that you're the default http adapter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论