Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'body' does not exist on type 'Request'

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)
    }
}
like image 638
LEMUEL ADANE Avatar asked Oct 17 '22 11:10

LEMUEL ADANE


2 Answers

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

I use it this way:

import * as bodyParser from 'body-parser';

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

(req: Request, res: Response) => {
    let address = req['body'];
}
like image 189
Val Avatar answered Oct 27 '22 11:10

Val


instead of

typings install express --save-dev

i did

npm install @typings/express --save-dev

and it gave me 'req.body'

like image 27
LEMUEL ADANE Avatar answered Oct 27 '22 11:10

LEMUEL ADANE