【问题标题】:getServerSideProps external api data fetching returns errorgetServerSideProps 外部 api 数据获取返回错误
【发布时间】:2021-12-08 17:12:20
【问题描述】:

我在 getServerSideProps 函数中的 http axios api 数据获取尝试总是返回错误。我已成功从 cockies 中恢复 token 和 userId,并尝试将它们作为参数传递以进行服务器 api 调用。

export const getServerSideProps: GetServerSideProps = async (ctx) => {

        try {
        const { userId, token } = ctx.req.cookies; 
        // console.log(userId)      
        // console.log(token)               
           
            const res = await api.get(`/users/show/${userId}`, {
              headers: { token },
         
            })
          console.log(res.data)
            const userData  = res.data;      

          if (!userData) {
            return {
              notFound: true,
            }
          }
          
            return {
              props: {  
                userData
         
              }
            }
          
        } catch (error) {
          return error
        }
          
          
      }

并不断收到同样的错误:

  Server Error
Error: Additional keys were returned from `getServerSideProps`. Properties intended for your component must be nested under the `props` key, e.g.:

    return { props: { title: 'My Title', content: '...' } }

Keys that need to be moved: config, request, response, isAxiosError, toJSON.

【问题讨论】:

  • return { notFound: true, } 返回 { props: { notFound: true } }
  • 这是什么await res.data;
  • 我添加了它,同样的错误仍在继续
  • @Djony 在 catch 块上添加 return { props: {} }
  • return errorreturn { props: {} } 不同,您不能只返回任何未包裹在道具之间的对象

标签: axios next.js fetch-api getserversideprops


【解决方案1】:

返回的对象应该一直在路上

return { props: {//在此处添加您的对象} }

export const getServerSideProps: GetServerSideProps = async (ctx) => {

        try {
        const { userId, token } = ctx.req.cookies; 
        // console.log(userId)      
        // console.log(token)               
           
            const res = await api.get(`/users/show/${userId}`, {
              headers: { token },
         
            })
          console.log(res.data)
            const userData  = res.data;      

          if (!userData) {
            return {
              return { props: { notFound: true } },
            }
          }
          
            return {
              props: {  
                userData
         
              }
            }
          
        } catch (error) {
          return { props: {} }
        }
          
          
      }

【讨论】:

  • 天啊!!!!你说对了 !!!!非常感谢!!但是现在我遇到了另一个问题,一个与我传递的 id 相关的打字稿错误。我可能需要输入 tiken 和 id 作为字符串来传递它们:TypeError: Cannot read properties of undefined (reading 'id')
  • @Djony 接受答案会有所帮助
  • 我仍然卡住了,有原始问题,不确定我是否正确获取 api 数据,无法在 getServerSideProps 中找到 axios api 调用的示例。我从cockies 获取令牌和用户ID,并试图作为参数传递,但不起作用: const res = await api.get(/users/show/${userId}, { params: { token }, // or params:{ // userId / / } })
  • 我修复了它,它正在获取数据,我只是添加了一些 header 基本配置: const res = await api.get(/users/show/${userId}, { headers: { Authorization: Bearer ${token}, }, })
猜你喜欢
  • 2020-06-08
  • 2021-04-21
  • 2023-02-15
  • 2015-11-21
  • 1970-01-01
  • 2018-09-23
  • 2021-06-28
  • 2017-05-23
  • 1970-01-01
相关资源
最近更新 更多