S.M_Emamian
S.M_Emamian

Reputation: 17383

Error serializing with getStaticProps - nextjs

This is my getTopGifts function:

export async function getTopGifts() {
const data = [{"id":1,"title":"test"}]

return data
}

but when I use above function on got error:

export async function getStaticProps() {
  // fetch list of gifts
  const gifts = getTopGifts()
  console.log(typeof(_gifts))
  return {
    props: {
      gifts: gifts
    },
  }
}

my error:

Error: Error serializing `.gifts` returned from `getStaticProps` in "/gifts".
Reason: `object` ("[object Promise]") cannot be serialized as JSON. Please only return JSON serializable data types.

Upvotes: 0

Views: 1191

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38807

You made getTopGifts an async function, try using await to resolve the value of getTopGifts. Without await it is a pending promise:

export async function getStaticProps() {
  // fetch list of gifts
  const gifts = await getTopGifts()
  console.log(typeof(gifts));
  return {
    props: {
      gifts,
    },
  }
}

Hopefully that helps!

Upvotes: 2

Related Questions