Getting `ReferenceError: body is not defined` even though I can see the body in console log

29 views Asked by At

This is a sanity webhook

import { revalidateTag } from "next/cache";
import { NextResponse } from "next/server";
import indexer from "sanity-algolia";
import { algoliaPostProjection } from "@/lib/queries";
import { sanityClient } from "@/lib/sanity.server";
import { algoliaServerClient } from "@/lib/algolia";
import { parseBody } from "next-sanity/webhook";

export async function POST(req) {
  try {
    const { body, isValidSignature } = await parseBody(
      req,
      process.env.SANITY_REVALIDATE_SECRET
    );

    console.log("body", body);

    if (!isValidSignature) {
      return new Response("Invalid Signature", { status: 401 });
    }

    if (!body?._type) {
      const message = "Bad Request";
      return new Response({ message, body }, { status: 400 });
    }

    revalidateTag(body._type);

    const algoliaIndex = algoliaServerClient.initIndex(
      process.env.ALGOLIA_INDEX
    );

    const sanityAlgolia = indexer(
      {
        post: {
          index: algoliaIndex,
          projection: algoliaPostProjection,
        },
      },
      (document) => document
    );

    await sanityAlgolia.webhookSync(sanityClient, body);

    return NextResponse.json({ status: 200, body });
  } catch (err) {
    console.error(err);
    return new Response({ err, body }, { status: 500 });
  }
}

body in console:

body {
  _type: 'post',
  ids: {
    created: [ null ],
    deleted: [ null ],
    updated: [ '8ecd2e6f-ced4-4e92-a6f3-b9885bc8eb6c' ],
    all: [ '8ecd2e6f-ced4-4e92-a6f3-b9885bc8eb6c' ]
  }
}

Error I am getting:

⨯ ReferenceError: body is not defined
    at x (/var/task/.next/server/app/api/revalidate/route.js:1:11124)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:42484
    at async eI.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:32486)
    at async eI.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:43737)
    at async Y (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:24556)
    at async Q.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1025)
    at async r3.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:507)
    at async r3.renderPageComponent (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:4780)
    at async r3.renderToResponseImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:5363)

Algolia is throwing an error which i believe is because the body is undefined

{
  name: 'ApiError',
  message: 'null value not allowed for objectID near line:1 column:61',
  status: 400,
  transporterStackTrace: [
    {
      request: [Object],
      response: [Object],
      host: [Object],
      triesLeft: 3
    }
  ]
}

I tried using const body = await req.json() but I am getting the same error ⨯ ReferenceError: body is not defined

Even with the error, 10% of the time, algolia is indexing the changes made in sanity and the cache revalidation is working 90% of the time.

0

There are 0 answers