TRPC:来自 createNextApiHandler 的 JSON 响应被中断

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

问题

  • 一些查询和突变现在会产生
    Unexpected end of JSON input
    Unterminated string in JSON at position
    错误
  • 这种情况是在使用不相关的更新进行重建后开始发生的。它可以在本地运行,但不能在主机上运行。

Stack(Turborepo)

  • api(Next.js v13.4.13)
    • 页面/api/trpc/[trpc].ts
  • 客户端(Next.js v13.4.13)
    • 使用 next.config.ts 中的重写连接到 api
  • tpc
    • 路由器
    • ...
  • 基础设施:render.com
  • orm:棱镜

代码

// apps/api/pages/api/trpc/[trpc].ts
export default createNextApiHandler({
  router: appRouter,
  createContext: createTRPCContext,
});

// apps/client/next.config.ts
async rewrites() {
    return [
      {
        source: "/api/trpc/:path*",
        destination: `${process.env.NEXT_PUBLIC_API}/api/trpc/:path*`,
      },
    ];
  },

回复样本

// status: 200 GET
[
    {
        "result": {
            "data": {
                "json": [
                    {
                        "items": [
                            {
                                "id": "uuid",
                                "label": "label",
                                "status": "statusA",
                                "history": [
                                    {
                                        "creatorA // => creatorAccount
// status: 200 POST
[
    {
        "result": {
            "data": {
                "json": {
                    "ok": true
                },
                "meta": {
                    "v

我尝试过的事情

  • 通过ThunderClient等直接连接api
    • 没有变化=>看来不是客户端配置错误
  • 设置响应头
    • 修复 Content-Type 和 Content-Encoding 没有任何区别
  • 修复 npm 模块的版本
    • 没有变化

确保所有查询/突变都像以前一样工作

prisma next render.com trpc
1个回答
0
投票

未设置CORS设置,我添加了,问题已解决。

© www.soinside.com 2019 - 2024. All rights reserved.