尝试使用邮递员测试时 trpc 给出错误

问题描述 投票:0回答:1

我正在使用 nextjs 应用程序路由器、trpc、prisma、zod。

尝试测试我的 trpc 端点:

http://localhost:3000/api/trpc/user.login
using Body- AW-JSON
{
    "username": "admin",
    "password": "CodingDojo2022!"
}

但我收到此错误:

 POST /api/trpc/user.login 400 in 15ms
❌ tRPC failed on user.login: [
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "undefined",
    "path": [],
    "message": "Required"
  }
]
{
    "error": {
        "json": {
            "message": "[\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"object\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]",
            "code": -32600,
            "data": {
                "code": "BAD_REQUEST",
                "httpStatus": 400,
                "stack": "TRPCError: [\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"object\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]\n    at inputValidatorMiddleware (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/unstable-core-do-not-import/middleware.mjs:56:19)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async callRecursive (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/unstable-core-do-not-import/procedureBuilder.mjs:160:32)\n    at async procedure (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/unstable-core-do-not-import/procedureBuilder.mjs:190:24)\n    at async eval (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/unstable-core-do-not-import/http/resolveResponse.mjs:207:30)\n    at async resolveResponse (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/unstable-core-do-not-import/http/resolveResponse.mjs:236:35)\n    at async fetchRequestHandler (webpack-internal:///(rsc)/./node_modules/@trpc/server/dist/adapters/fetch/fetchRequestHandler.mjs:39:12)\n    at async /Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:55038\n    at async ek.execute (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45808)\n    at async ek.handle (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:56292)\n    at async doRender (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:1377:42)\n    at async cacheEntry.responseCache.get.routeKind (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:1587:40)\n    at async DevServer.renderToResponseWithComponentsImpl (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:1507:28)\n    at async DevServer.renderPageComponent (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:1931:24)\n    at async DevServer.renderToResponseImpl (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:1969:32)\n    at async DevServer.pipeImpl (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:920:25)\n    at async NextNodeServer.handleCatchallRenderRequest (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/next-server.js:272:17)\n    at async DevServer.handleRequestImpl (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/base-server.js:816:17)\n    at async /Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/dev/next-dev-server.js:339:20\n    at async Span.traceAsyncFn (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/trace/trace.js:154:20)\n    at async DevServer.handleRequest (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/dev/next-dev-server.js:336:24)\n    at async invokeRender (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/lib/router-server.js:174:21)\n    at async handleRequest (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/lib/router-server.js:353:24)\n    at async requestHandlerImpl (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/lib/router-server.js:377:13)\n    at async Server.requestListener (/Users/coding-workspace/Projects/Project440/onlinestore/node_modules/next/dist/server/lib/start-server.js:141:13)",
                "path": "user.login",
                "zodError": {
                    "formErrors": [
                        "Required"
                    ],
                    "fieldErrors": {}
                }
            }
        }
    }
}

这意味着输入的类型不正确,但对我来说一切看起来都是正确的。

这是我的代码:

login: publicProcedure
    .input(
      z.object({
        username: z.string(),
        password: z.string(),
      })
    )
    .mutation(async ({ input, ctx }) => {
      const { username, password } = input;
      const user = await db.user.findUnique({ where: { username } });

      if (!user || !(await verifyPassword(password, user.password))) {
        throw new TRPCError({
          code: "UNAUTHORIZED",
          message: "Invalid username or password",
        });
      }
..
.
.
.

我在邮递员中的输入是原始 JSON,带有用户名和密码,在登录端点中输入是用户名和密码都是字符串的 ZOD 对象。

在我的邮递员中,我在标头中设置了内容类型应用程序/JSON 但仍然无法工作。

next.js postman prisma zod trpc
1个回答
0
投票

美好的一天,我面临着同样的问题,我已经解决了。 这是我所做的步骤:

  • 我已在浏览器中安装并启动
    Postman Interceptor Proxy
    以捕获
    POST
    发送的
    Prisma
    方法:

interceptor_reference


  • 将架构添加到我的请求中,并且我已成功将其插入数据库中。

来自:

before_modification_from_interceptor_reference

对此:

after_modification_from_interceptor_reference

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.