Get a parameter value from a GET request url. "TypeError: Cannot read property"
Get a parameter value from a GET request url. "TypeError: Cannot read property"
我只是想从 get 请求的 url 路由参数中获取数据。我很确定我以前有这个工作,而且我没有改变任何东西。不确定发生了什么。
我一直在整个代码中调用 console.log,只是想看看会出现什么。但他们甚至从未被召唤过。仅显示错误消息。
我正在使用邮递员,这是我正在发出的获取请求:
http://localhost:3333/users/accountidfind/1
这是我的 index.ts 文件中的路由器:
app.use('/users', userRouter);
这是错误来源的代码段:
userRouter.get('/accountidfind/:account_id', async (req: Request, resp: Response) => {
//console.log(`retrieving user with id ${(<any>+req).params.account_id}`);
const account_id = (<any>+req).params.account_id;
这是整个代码:
import { Request, Response } from "express";
import * as express from "express";
import * as userDao from "../dao/user-dao";
userRouter.get('/accountidfind/:account_id', async (req: Request, resp: Response) => {
//console.log(`retrieving user with id ${(<any>+req).params.account_id}`);
const account_id = (<any>+req).params.account_id;
try {
const user = await userDao.getUserById(account_id);
if (user !== undefined) {
resp.json(user);
} else {
resp.sendStatus(400);
}
} catch (err) {
resp.sendStatus(500);
}
});
这是我遇到的错误:
(node:9532) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'account_id' of undefined
at C:\GitFolder\roqq\server\routers\user-router.ts:95:41
at Generator.next (<anonymous>)
at C:\GitFolder\roqq\server\routers\user-router.ts:27:71
at new Promise (<anonymous>)
at __awaiter (C:\GitFolder\roqq\server\routers\user-router.ts:23:12)
at C:\GitFolder\roqq\server\routers\user-router.ts:94:85
at Layer.handle [as handle_request] (C:\GitFolder\roqq\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\GitFolder\roqq\server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\GitFolder\roqq\server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\GitFolder\roqq\server\node_modules\express\lib\router\layer.js:95:5)
(node:9532) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 6)
我也不明白错误消息是怎么说异步函数没有 catch 块的,而实际上确实有一个 catch 块。
问题是您的转换 (<any>+req)
转换为 (+req)
将 req
解析为数字 - 因此您没有 .params
要解决您的问题,请将 (<any>+req)
替换为 (req as any)
或 (<any>req)
我只是想从 get 请求的 url 路由参数中获取数据。我很确定我以前有这个工作,而且我没有改变任何东西。不确定发生了什么。
我一直在整个代码中调用 console.log,只是想看看会出现什么。但他们甚至从未被召唤过。仅显示错误消息。
我正在使用邮递员,这是我正在发出的获取请求:
http://localhost:3333/users/accountidfind/1
这是我的 index.ts 文件中的路由器:
app.use('/users', userRouter);
这是错误来源的代码段:
userRouter.get('/accountidfind/:account_id', async (req: Request, resp: Response) => {
//console.log(`retrieving user with id ${(<any>+req).params.account_id}`);
const account_id = (<any>+req).params.account_id;
这是整个代码:
import { Request, Response } from "express";
import * as express from "express";
import * as userDao from "../dao/user-dao";
userRouter.get('/accountidfind/:account_id', async (req: Request, resp: Response) => {
//console.log(`retrieving user with id ${(<any>+req).params.account_id}`);
const account_id = (<any>+req).params.account_id;
try {
const user = await userDao.getUserById(account_id);
if (user !== undefined) {
resp.json(user);
} else {
resp.sendStatus(400);
}
} catch (err) {
resp.sendStatus(500);
}
});
这是我遇到的错误:
(node:9532) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'account_id' of undefined
at C:\GitFolder\roqq\server\routers\user-router.ts:95:41
at Generator.next (<anonymous>)
at C:\GitFolder\roqq\server\routers\user-router.ts:27:71
at new Promise (<anonymous>)
at __awaiter (C:\GitFolder\roqq\server\routers\user-router.ts:23:12)
at C:\GitFolder\roqq\server\routers\user-router.ts:94:85
at Layer.handle [as handle_request] (C:\GitFolder\roqq\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\GitFolder\roqq\server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\GitFolder\roqq\server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\GitFolder\roqq\server\node_modules\express\lib\router\layer.js:95:5)
(node:9532) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 6)
我也不明白错误消息是怎么说异步函数没有 catch 块的,而实际上确实有一个 catch 块。
问题是您的转换 (<any>+req)
转换为 (+req)
将 req
解析为数字 - 因此您没有 .params
要解决您的问题,请将 (<any>+req)
替换为 (req as any)
或 (<any>req)