Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript localStorage.getItem()在graphql查询之前返回未定义_Javascript_Reactjs_Graphql_Auth0 - Fatal编程技术网

Javascript localStorage.getItem()在graphql查询之前返回未定义

Javascript localStorage.getItem()在graphql查询之前返回未定义,javascript,reactjs,graphql,auth0,Javascript,Reactjs,Graphql,Auth0,使用graphql、react和auth0 当用户从auth0登录时,localStorage.setItem('user\u email',authResponse.email) 然后他们进入主页在那里我有一个graphql查询: export default graphql(USER_QUERY, { name: 'userQuery', skip: () => (localStorage.getItem('user_email') === null ||

使用graphql、react和auth0

用户从auth0登录时,
localStorage.setItem('user\u email',authResponse.email)

然后他们进入主页在那里我有一个graphql查询:

 export default graphql(USER_QUERY, {
       name: 'userQuery',
       skip: () => (localStorage.getItem('user_email') === null ||
                    localStorage.getItem('user_email') === 'undefined'),
       options: ()=>{
          return { variables: {email: localStorage.getItem('user_email')}}
       })(Component)
问题是,查询永远不会执行,因为
getItem()
返回电子邮件太晚了。相反,我得到一个错误,它试图发送
未定义的

我怎样才能等待本地存储?一个
承诺
可以事后解决吗?我是否应该以某种方式使用
componentDidMount(){}

我尝试过获取
this.props.userQuery.refetch()
,但未成功,我将使用新变量对
设置超时()
进行refetch,但无法正确写入或使其工作

编辑下面是用于设置会话的Auth.js文件:

setSession(authResult) {
    //return new Promise((resolve, reject)=> {
    // Set the time that the access token will expire at
    let expiresAt = JSON.stringify(
        authResult.expiresIn * 1000 + new Date().getTime()
    );
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('expires_at', expiresAt);
    localStorage.setItem('user_email', authResult.idTokenPayload.email)

    history.replace('/dashboard/home')
}

我在一些参考资料中发现,您不能使用graphql查询进行异步调用,因此它将始终被跳过。相反,我在
componentDidMount()
上调用了查询,如下所示:

componentDidMount(){
        const checkEmailAndQuery = () => {
            if(this.state.userId === null) {
                if (localStorage.getItem('user_email') !== null) {
                    this.props.client.query({
                        query: USER_QUERY,
                        variables: {email: localStorage.getItem('user_email')}
                    }).then(({data}) => {
                        this.setState({userId: data.user.id})
                    })
                } else {
                    setTimeout(checkEmailAndQuery, 200)
                }
            }
        }
        checkEmailAndQuery()
    }

你能发布登录调用的代码并转换到主页吗?我不知道你为什么会有
localStorage.getItem('user\u email')==='undefined'
条件,你真的希望看到
'undefined'
字符串吗?还是应该是
localStorage.getItem('user\u email')===undefined
?另外,如果您只执行
localStorage.setItem()
localStorage.removietem()
操作,则它应该是
null
或您设置的字符串值。@AdamPatterson我刚刚添加了它,未定义的部分是确保它不会尝试以未定义作为结果进行查询,因为如果它没有,查询没有发生,因为它只是被完全跳过了。@KevinDanikowski尽管像tudorpavel说的那样,它应该是
NULL
。Localstorage返回
NULL
,未定义。