反应路线终点中的数据加载问题

时间:2019-01-11 06:31:06

标签: reactjs firebase graphql

    import React, { Component } from 'react';
    //import fire from '../firebase/Fire';
    import { ApolloProvider } from 'react-apollo';
    import  ApolloClient  from 'apollo-boost';
    import  Restaurants  from '../restaurants/Restaurants';
    import { auth } from '../firebase';
    import  firebase  from  '../firebase/firebase'


    export const client = new ApolloClient({
      uri : 'https://eatmealfirstadminapi.leafycode.com/',
      request: async operation => {
      if(firebase.auth().currentUser){
          const token=await firebase.auth().currentUser.getIdToken(/* forcefresh */true);



         operation.setContext({
            headers:{
              CountryCode: "LK",
              authorization: token
            }
         });

        }
      }
    });

    class Home extends Component {
        constructor(props) {
            super(props);
            this.logout = this.logout.bind(this);
            //console.log(this.authLink.headers.authorization);
            //this.authLink.headers.authorization=this.props.token;
        }

        logout() {
            firebase.auth().signOut();
            this.props.history.push("/authentication/login2");
        }

        render() {
            return (
                <ApolloProvider client={client}  >
                <div className="container">
                  <div>
                    <h1>Restaurant List</h1>
                    <button onClick={this.logout}>Logout</button>

                    <Restaurants />
                  </div>
                </div>
               </ApolloProvider>
            );

        }

    }

export default Home;

componet screen shot

我正在使用一些模板来创建管理面板,有firebase登录页面,然后如果要检查天气,则先登录还是不登录,然后在登录到仪表板之前登录,否则浏览到登录页面,所以当我进入时到仪表板,我还有另一个侧边栏链接,可转到仪表板以外的其他路线终点,因此一旦转到仪表板,然后单击该其他路线终点链接并在此处查看那些详细信息。但是当我刷新该页面时,便可以看到数据在那里,它显示错误消息,例如[GraphQL error]: Message: Not Authorised!, Location: [object Object], Path:餐厅,但再次转到该仪表板终点,然后转到其他链接,它显示那里的数据,我需要知道为什么会发生,并且需要解决该问题

0 个答案:

没有答案