关于label api 获取的问题

来源:10-10 完成issue搜索功能的开发

SpicaII

2020-06-01

老师,你好 我遇到一个问题 是关于api labels的请求报403的问题。
图片描述
我不太明白为什么isServer在这里为什么会是false。
这个是Issues 的getInitialProps的代码:

Issues.getInitialProps = async ({ ctx }) => {

    const { owner, name } = ctx.query

    const  issuesResp = await api.request({
        url: `/repos/${owner}/${name}/issues`
    },
        ctx.req,
        ctx.res,
      )
    
    const labelsResp = await api.request({
            url: `/repos/${owner}/${name}/labels`
        },
        ctx.req,
        ctx.res,
    )

    return {
        issues: issuesResp.data,
        labels: labelsResp.data,
    }
}

关键是issues走的也是**/github/**repos/owner/name/issues
是在是有点想不清楚,拜托老师解惑,谢谢!

以下是截图补充:
图片描述
然后跳转到如下页面:
图片描述

const isServer = typeof window === 'undefined'
async function request({ method = 'GET', url, data, headers }, req, res) {
    if(!url){
        throw Error('url must provide')
    }
    console.log("isServer value is: " + isServer)
    if(isServer){
        const session = req.session
        const githubAuth = session.githubAuth || {}
        const headers = {}
        if(githubAuth.access_token){
            headers['Authorization'] = `${githubAuth.token_type} ${githubAuth.access_token}`
        }
        console.log(url)

        return await requestGithub(method, url, data, headers)
    } else {
        console.log('label comes here')
        // search/respos
        return await axios({
            method,
            url: `/github${url}`,
            data,
            headers
        })
    }
}

补充完毕

写回答

1回答

Jokcy

2020-06-01

我好像没看到isServer的判断,你可以试着走一下debugger。另外有具体的报错信息么

0
3
SpicaII
回复
Jokcy
跳转时没有变叉叉。
2020-06-26
共3条回复

全栈进阶课程 React16.8+Next.js+Koa2一步到位开发Github

学习React/Next.js服务端渲染SSR同构设计方案,理解OAuth登录体系的实现原理

651 学习 · 311 问题

查看课程