Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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
Reactjs 如何在Apollo中返回用户对象的id属性?_Reactjs_Graphql_Auth0_Apollo_Graphcool - Fatal编程技术网

Reactjs 如何在Apollo中返回用户对象的id属性?

Reactjs 如何在Apollo中返回用户对象的id属性?,reactjs,graphql,auth0,apollo,graphcool,Reactjs,Graphql,Auth0,Apollo,Graphcool,在查询用户模型的React组件中放置以下内容时,我能够获得graphQL查询的整个用户对象,包括id属性: console.log(this.props.data.user) 但当我尝试从代码访问id属性时: console.log(this.props.data.user) // undefined console.log(this.props.data.user.id) // error cannot get property of undefined 我的第一个猜测是,这是一个安

在查询用户模型的React组件中放置以下内容时,我能够获得graphQL查询的整个用户对象,包括id属性:

console.log(this.props.data.user)
但当我尝试从代码访问id属性时:

console.log(this.props.data.user) // undefined 
console.log(this.props.data.user.id)
 // error cannot get property of undefined 
我的第一个猜测是,这是一个安全特性;我正在使用Auth0和GraphTool

但我可能只是在以一种倒退的方式来处理这个问题。如果是这样,以正确方式访问用户id的任何帮助都将不胜感激

谢谢

这将在中介绍

使用Auth0获取签名JWT
user
查询返回当前经过身份验证的用户。因此,首先我们必须考虑如何确定经过身份验证的用户

当前的最新技术是将它们作为
授权
头传递

在Auth0锁中输入有效凭据后,它返回一个用您的秘密Auth0密钥签名的JWT。这个签名的JWT被发送到GraphQL服务器,在那里我们将使用Auth0密钥来验证它,如果它属于有效的用户,则请求将被验证

使用Apollo客户端设置
授权
标题 因此,我怀疑您只是没有传递有效的
授权
头。使用Apollo,您可以使用它来确保传递令牌(如果令牌存在)。注意,我们将使用本地存储来存储Auth0锁中的令牌:

const networkInterface = createNetworkInterface({ uri: 'https://api.graph.cool/simple/v1/__PROJECT_ID__' })

// use the auth0IdToken in localStorage for authorized requests
networkInterface.use([{
  applyMiddleware (req, next) {
    if (!req.options.headers) {
      req.options.headers = {}
    }

    // get the authentication token from local storage if it exists
    if (localStorage.getItem('auth0IdToken')) {
      req.options.headers.authorization = `Bearer ${localStorage.getItem('auth0IdToken')}`
    }
    next()
  },
}])

const client = new ApolloClient({ networkInterface })
检查或查看其工作原理


您可能也对它感兴趣。

谢谢,但实际上我正在使用您建议的github项目。它正在处理身份验证、查询、变异等所有工作。您能为代码添加更多上下文吗?可能您正在访问
数据之前的
数据。在
数据之前加载
。加载
是错误的,也就是说。谢谢两者..这就是问题所在。