Next.js Discord

Discord Forum

Help using getServerSideProps.

Unanswered
Will posted this in #help-forum
Open in Discord
Anyone able to help me identify why getServerSideProps returns undefined, but if I run the api call in my footer function, it gets data?

import React from "react";
import Ticker from "./ticker";
import { api } from "~/utils/server";

type Props = {
  postCount: number;
};

export const getServerSideProps = async () => {
  try {
    const data = await api.post.get();
    const postCount = data.length;
    console.log(data);
    return { props: { postCount }, revalidate: 5 };
  } catch (error) {
    console.error("Failed to fetch post count:", error);
    return { props: { postCount: 0 }, revalidate: 5 };
  }
};

export default async function Footer({ postCount }: Props) {
  console.log(postCount);
  return (
    <div className="relative mx-auto max-w-6xl overflow-hidden whitespace-nowrap px-4 py-4 sm:px-6 lg:px-8">
      <Ticker stats={[`${postCount > 0 ? postCount : "..."} Job Listings`]} />
    </div>
  );
}

3 Replies