小编Jos*_*lho的帖子

如何在 Typescript 中正确扩展请求的标头

我需要userIdin req.headers,但我无法正确输入,我该怎么做?

首先我尝试这样做:

  interface ISpot{
    thumbnail: File,
    company: string,
    price: number,
    techs: string
  }

  interface IReqCustom<T> extends Request{
    body: T,
  }

  export default {
    async store (req: IReqCustom<ISpot>, res: Response): Promise<Response> {
      const { filename } = req.file
      const { company, techs, price } = req.body
      const { userId } = req.headers

      const user = await User.findById(userId)

      if (!user) {
        return res.status(400).json({ error: 'User doesn\'t exist' })
      }

      const spot = await Spot.create({
        user: userId,
        thumbnail: filename, …
Run Code Online (Sandbox Code Playgroud)

types backend node.js express typescript

5
推荐指数
1
解决办法
3347
查看次数

标签 统计

backend ×1

express ×1

node.js ×1

types ×1

typescript ×1