繁体   English   中英

如何在 node.js 中使用 typescript 从快递中获取 GET 参数?

[英]How to get GET params from express with typescript in node.js?

我正在使用 node.js + express + typescript,我想获得 GET 参数。 到目前为止我有这个

app.get('/top_games', (req, res) => {
    const page = req.route.page;
    const offset = req.route.offset;
    const game_data = DAO_GAME.GetTopGuilds(100, 0);
    res.send({
        total : game_data[0],
        data : game_data[1]
    });
});

但 eslint 抱怨

    const page = req.route.page;
    const offset = req.route.offset;

Unsafe assignment of an any value.eslint@typescript-eslint/no-unsafe-assignment
Unsafe member access .page on an any value.eslint@typescript-eslint/no-unsafe-member-access

我怎样才能解决这个问题?

修好了,我必须做

const page = parseInt(req.query.page as string, 10);
const page_size = parseInt(req.query.page_size as string, 10);

@types/express@4.17.2开始, Request类型使用 generics 我们可以使用它自己输入解析的查询参数:

import { Request } from 'express';

type QueryParams = {
    page: string;
    page_size: string;
};

app.get('/top_games', (req: Request<{}, {}, {}, QueryParams>, res) => {
    const page = parseInt(req.route.page, 10);
    const offset = parseInt(req.route.offset, 10);

    // ...
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM