No Message in cron job log

import { NextResponse } from “next/server”;

export async function GET(req) {

  • const authHeader = req.headers.get(“Authorization”);*

  • if (authHeader !== Bearer ${process.env.CRON_SECRET}) {*

  • return new NextResponse(“Unauthorized”, { status: 401 });*

  • }*

  • try {*

  • const response = await fetch(*

  •  process.env.NEXT_PUBLIC_BACKEND_SERVER + "/items/0/2/all",*
    
  •  {*
    
  •    method: "GET",*
    
  •  }*
    
  • );*

  • return NextResponse.json({*

  •  message: "Cron job executed successfully",*
    
  • });*

  • } catch (error) {*

  • console.error(“Error triggering website:”, error);*

  • return new NextResponse(“Failed to trigger external website”, {*

  •  status: 500,*
    
  • });*

  • }*
    }

/// I DONT SEE MESAGES IN LOG ONLY STATUS

I can see the issue with your cron job route handler. The problem is that you’re not returning the data from your fetch request, and there’s a syntax error in your Authorization header check

import { NextResponse } from "next/server"

export async function GET(req: Request) {
  const authHeader = req.headers.get("Authorization")

  // Fix the template literal syntax
  if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
    console.log("Unauthorized access attempt")
    return new NextResponse("Unauthorized", { status: 401 })
  }

  try {
    console.log("Cron job started")

    const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_SERVER}/items/0/2/all`, {
      method: "GET",
    })

    // Get the actual data from the response
    const data = await response.json()

    console.log("Cron job completed successfully", data)

    return NextResponse.json({
      message: "Cron job executed successfully",
      data: data, // Include the fetched data in the response
    })
  } catch (error) {
    console.error("Error triggering website:", error)

    return new NextResponse("Failed to trigger external website", {
      status: 500,
    })
  }
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.