gpt4 book ai didi

express - 无法读取未定义的属性 'context' - GraphQL

转载 作者:行者123 更新时间:2023-12-03 21:57:21 24 4
gpt4 key购买 nike

我正在使用 Typescript、Express、TypeORM、GraphQL 和 TypeGraphQL 来构建一个允许用户登录的小应用程序。

但是,当我点击我的测试查询时 bye在 GraphQL 操场上,我得到:

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 中未定义

最佳答案

解决感谢:https://github.com/MichalLytek/type-graphql/issues/433

1) 进入 ./tsconfig.json
2) 更改 "target": "es5""target": "es6"

关于express - 无法读取未定义的属性 'context' - GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61291222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com