‘cookies’ 属性在类型 ‘Request’ 上不存在。

huangapple go评论88阅读模式
英文:

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

  1. import { Controller, Get, Render, Req } from '@nestjs/common';
  2. @Controller()
  3. export class AppController {
  4. @Get()
  5. @Render('home')
  6. getHello(@Req() req: Request) {
  7. return { text: req.cookies['id'] };
  8. }
  9. }

the problem is that type Request from express does not have cookies. So i get this error.

  1. src/app.controller.ts:11:24 - error TS2339: Property 'cookies' does not exist on type 'Request'.
  2. 11 return { text: req.cookies['id'] };
  3. ~~~~~~~

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

  1. import { Controller, Get, Render, Req } from '@nestjs/common';
  2. @Controller()
  3. export class AppController {
  4. @Get()
  5. @Render('home')
  6. getHello(@Req() req: Request) {
  7. return { text: req.cookies['id'] };
  8. }
  9. }

the problem is that type Request from express does not have cookies. So i get this error.

  1. src/app.controller.ts:11:24 - error TS2339: Property 'cookies' does not exist on type 'Request'.
  2. 11 return { text: req.cookies['id'] };
  3. ~~~~~~~

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.

huangapple
  • 本文由 发表于 2023年2月8日 19:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定