如何在React中一个又一个地调用获取API?

时间:2018-09-22 11:15:31

标签: javascript reactjs rest express fetch

我有两个用于OAuth后端的API:

app.get('/auth/google',passport.authenticate('google', {
                scope: ['profile']
            })
        )



app.get('/auth/google/callback',passport.authenticate('google', { failureRedirect: '/login' }),
        (req, res) => {
            console.log(req.user.google)

            res.json(req.user.google)
        }
    )

前端(JSX)

 //All under React class
 getData = () => {
       fetch('http://localhost:8080/auth/google/callback',{
       mode: 'no-cors',
       method: 'get'
      })
   .then(response => response.text())
   .then(text => console.log(text))
}

 callFirstApi = () => {
       fetch('http://localhost:8080/auth/google',{
       mode: 'no-cors',
       method: 'get'
  })
 .then(this.getData()) 
} 



 render(){
     return(
          <div className = "" >
       <input onClick = { this.callFirstApi }

  type = "submit"
  value = "Register with Google" />
  </div> 
       )
  }

_______________________________________________________________________________ 终端(控制台)

InternalOAuthError:无法获取访问令牌     在Strategy.OAuthStrategy._createOAuthError GET / auth / google / callback?oauth_token = 81--b09b6480-9faa-45ee-ac78-1934190&oauth_verifier = 28583 500 289.147 ms-1295

{id:'q5iPMrRJab',

令牌:“ bfc0bb7-f7c5-4ef5-b97c-0b3e77bf550a”,

tokenSecret:'d473705-ddd3-487b-8838-849a476b29ed',

名字:“ Some name”,

lastName:“某些名称”

}

GET / auth / google / callback?oauth_token = 81--d2c05-1af2-4dff-b228-98d88f2caddf&oauth_verifier = 59719 200 1639.049 ms-210

_______________________________________________________________________________

由于 / auth / google 没有给出任何响应,因此会触发其回调 API,这就是为什么我试图在 auth / google 即可获取数据。 但是很奇怪,在我的控制台中,我遇到了第一个令牌错误,然后是用户数据,并且没有将任何数据传递到前端

如何实现这种嵌套的获取api情况并发送以响应组件?

0 个答案:

没有答案