【问题标题】:nextjs api not working when fetching through isomorphic-unfetch通过 isomorphic-unfetch 获取时 nextjs api 不起作用
【发布时间】:2021-01-03 17:46:29
【问题描述】:

我没有在道具中获取我的数据 (notes),当我使用 console.log(notes) 时,它给出了未定义的数据。 我的后端 api 也在 /pages/api/notes/index.js 的同一个项目中 我调用的api是'http://localhost:3000/api/notes'

当我在邮递员上测试它时,即使我的 api 也能正常工作。但是在前端,数据不可用

请查看此问题。

【问题讨论】:

    标签: javascript reactjs fetch next.js isomorphic-fetch-api


    【解决方案1】:

    我相信你只是有一个错字。 Index.getinitialProps 应该是 Index.getInitialProps。请注意,首字母的“i”是大写的。

    另外作为提示,您并不明确需要http://localhost:3000,如果您只是使用/api/notes 调用它,它将正确解决请求。这样,当您部署应用程序时,您不必返回并为您的数据获取进行大量更改。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2019-08-20
      • 1970-01-01
      • 2022-01-24
      • 2021-08-04
      • 1970-01-01
      • 2021-06-05
      • 2020-07-27
      • 2023-02-03
      相关资源
      最近更新 更多