属性"body"在类型"Request"上不存在 [英] Property 'body' does not exist on type 'Request'

查看:166
本文介绍了属性"body"在类型"Request"上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请求类型的 req 变量对属性 body 没有智能.这是由于打字造成的吗?

The req variable of Request type has no intellisense for property body. Is this due to the typings?

import { Request, Response } from 'express'
import { ok, bad } from './responses'

export const signIn: async (req: Request, res: Response) => {
    try {
        const { name, pword } = req.body // body is not recognized
        const data = auth.signIn(name, password)
        ok(res, data)
    } catch (error) {
        bad(res, error)
    }
}

推荐答案

body-parser 已从表达式4 中删除. a>进入单独的项目, 因此不会有任何类型定义.

body-parser had been removed from express 4 into separate project, so there won't be any type definition about it.

我以这种方式使用它:

import * as bodyParser from 'body-parser';

let router: Router = express.Router();
router.use(bodyParser.text());

(req: Request, res: Response) => {
    let address = req['body'];
}

这篇关于属性"body"在类型"Request"上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆