【发布时间】:2020-08-01 02:40:40
【问题描述】:
我正在使用 Typescript、Express、TypeORM、GraphQL 和 TypeGraphQL 构建一个允许用户登录的小应用程序。
但是,当我在 GraphQL 操场上点击我的测试查询 bye 时,我得到:
Cannot read property 'context' of undefined
"TypeError: Cannot read property 'context' of undefined",
" at new exports.isAuth // isAuth is a JS file I wrote
MyContext.js
import { Request, Response } from "express";
export interface MyContext {
req: Request;
res: Response;
payload?: { userId: string };
}
isAuth.js
import { MiddlewareFn } from "type-graphql";
import { verify } from "jsonwebtoken";
import { MyContext } from "./MyContext";
export const isAuth: MiddlewareFn<MyContext> = ({ context }, next) => {
const authorization = context.req.headers["authorization"];
if (!authorization) {
throw new Error("not authorized");
}
...
用户解析器
@Query(() => String)
@UseMiddleware(isAuth)
bye(@Ctx() { payload }: MyContext) {
console.log(payload);
return `your user id is: ${payload!.userId}`;
}
我不确定为什么context 在文件isAuth.js 中未定义
【问题讨论】:
-
本·阿瓦德,amirite?
标签: express graphql typegraphql