mrnja
mrnja

Reputation: 11

How can I get data from getStaticProps?

I have an issue with getStaticProps, and news are always "undefined" for some reason. I think that's not rendering at all.

type NewsItem = {
  id: string;
  published: string;
  author: string;
  avatar: string;
  title: string;
  background: string;
  subtitle: string;
  category: string;
};

 export const getStaticProps: GetStaticProps<{
  news: NewsItem[];
}> = async () => {
  const client = await MongoClient.connect(`${process.env.REACT_MONGO_URL}`);
  const db = client.db();

 

  const newsCollection = db.collection("news");

  const news1 = await newsCollection.find().toArray();

  console.log("news2:", news1);

  client.close();

  return {
    props: {
      news: news1.map((nw) => ({
        published: nw.published,
        author: nw.author,
        avatar: nw.avatar,
        title: nw.title,
        background: nw.background,
        subtitle: nw.subtitle,
        category: nw.category,
        id: nw._id.toString(),
      })),
    },
  };
};

I tried with DUMMY data and without TypeScript, and the problem is same.

const News = ({ news }: InferGetStaticPropsType<typeof getStaticProps>) => {
  console.log("news1", news);
...
export default News;

I learned on an older version, and back then everything was fine, but now with the latest version of Next.js, it's not working.

Upvotes: 1

Views: 21

Answers (0)

Related Questions