简体   繁体   English

next.js 未在 getStaticProps() 上返回项目

[英]next.js is not returning items on getStaticProps()

I am trying to load items to my next.js page and it will fail:我正在尝试将项目加载到我的 next.js 页面,它会失败:

import {getadminInfo} from '../../dataFetch/adminInfo'
import {addItem} from '../../dataFetch/catalog'
import {useState} from "react"
import { getList } from '../../dataFetch/catalogList'

export async function getStaticProps() {
  const adminData = await getadminInfo()
  const catlist = await getList()

  return {
        props: {
              catlist, 
              adminData
        }
  }
}


export default function Main({allPostsData, adminData, catlist}) {
}

My function is: export function getList() {我的 function 是: export function getList() {

const pageInfo = {
    page_size : "10",
    page:"1"
}

const url = "http://localhost:8000/api/catalog/list?page_size="+pageInfo.page_size+"&page="+pageInfo.page;

try {
    fetch(url, {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        fData=JSON.parse(JSON.stringify(data.response))
        console.log("Returned catalog")
        return fData 
    })
    .catch(error => console.log(error))  
  } catch (err) {
    console.log(err)
  }
}

The API works and I get the right info back but I cannot load it to the page: API 工作正常,我得到正确的信息,但我无法将其加载到页面:

Error: Error serializing .catlist returned from getStaticProps in "/admin/main".错误:序列化从“/admin/main”中的getStaticProps返回的.catlist时出错。 Reason: undefined cannot be serialized as JSON.原因: undefined不能序列化为JSON。 Please use null or omit this value.请使用null或省略此值。

I found the issue.我发现了这个问题。 I did not implement the fetch correctly.我没有正确实现提取。 It should have been async.它应该是异步的。 The reason I did not get the info is because nothing was returned.我没有得到信息的原因是没有返回任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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