我正在尝试为我的变量分配一个类型,该变量从express'请求查询中获取其值。 req.query 的类型为“QueryString.ParsedQs”
例如 req.query.accountId 的类型为“string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[]”
我如何从 req.query.accountId 中提取值以使其始终为字符串类型?
示例代码:
import { Request, Response } from "@feathersjs/express";
class Xyz {
public myMethod( req: Request, res: Response ){
const accountId =req.query.accountId;/* type is "string | QueryString.ParsedQs | string[] | QueryString.ParsedQs[]"*/
}
}
您可以检查“string”是否通过:
if (typeof accounId === 'string') {
// do stuff
}
或者将其转换(强制,我不建议)将其转换为字符串:
accountId = String(req.query.accountId);
// Or shorter
accountId = '' + req.query.accountId;
如果转换前它不是字符串,您最终会得到类似 ‛[Object array]‛ 的结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)