简体   繁体   中英

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM