Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 使用jwt响应本机获取api错误_Reactjs_React Native_Jwt_Fetch_Json Web Token - Fatal编程技术网

Reactjs 使用jwt响应本机获取api错误

Reactjs 使用jwt响应本机获取api错误,reactjs,react-native,jwt,fetch,json-web-token,Reactjs,React Native,Jwt,Fetch,Json Web Token,我正在尝试在我的react本机应用程序中实现Jsonwebtoken。我在他们的官方网站上观看了视频教程,效果很好。但是,每次尝试进行api调用时,我都会收到错误“UnauthorizedError:未找到授权令牌”。api调用了所有预期的工作,除了我仍然会得到这样的错误。我在postman中尝试了相同的api调用,它们不会给我错误。我发现这可能是因为我没有在api调用后传递回令牌 这里是我启动快速jwt的地方 var expressJWT = require('express-jwt') ap

我正在尝试在我的react本机应用程序中实现Jsonwebtoken。我在他们的官方网站上观看了视频教程,效果很好。但是,每次尝试进行api调用时,我都会收到错误“UnauthorizedError:未找到授权令牌”。api调用了所有预期的工作,除了我仍然会得到这样的错误。我在postman中尝试了相同的api调用,它们不会给我错误。我发现这可能是因为我没有在api调用后传递回令牌

这里是我启动快速jwt的地方

var expressJWT = require('express-jwt')
app.use(expressJWT({secret: 'I like different tastes'}).unless({path:['/api/users/login', '/api/users/register', '/api/users/register/facebook']}))
这里是我登录并创建jwt并将其传回客户端的地方

login: function(req,res){
if (req.body.purpose == 'login'){
  User.findOne({'localUser.email': req.body.email,}, function(err, user){
    if (err){
      console.log(err)
      res.status(500).json('error')
    }
    else if(user == null){
      res.status(404).json('not found');
    }
    else{
      if (passwordHash.compareSync(req.body.password, user.localUser.password)){
        var myToken = jwt.sign({username: user.localUser.name}, 'I like different tastes')
        var tmpUser = user.toJSON()
        tmpUser.token = myToken
        res.status(200).json(tmpUser);
      }
      else{
        res.status(401).json('wrong credentials')
      }
    }
  })
}
else{
  res.status(400).json('You are hacked bro')
}
这是我登录后进行的api调用的一个示例

fetchPost() {
var _this = this
return fetch(uri.network.uri + '/api/posts/fetch', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer '+this.props.activeUser.token,
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      purpose: 'fetchPost',
      latitude: this.state.currentRegion.latitude,
      longitude: this.state.currentRegion.longitude,
      maxSearch: MAX_SEARCH
    })
  })
  .then((response) => {
    if (response.status != 200){
      Alert.alert(
       'Sorry',
       'Unexpected errors!'
      );
      return
    }
    else{
      return response.json();
    }
  })
  .then((responseJson)=>{
    console.log(responseJson)
  })
  .catch((error) => {
    console.error(error);
  });
}

this.props.activeUser.token是我从服务器端的第二个函数传回的令牌 它工作得很好,我得到了我想要的数据。我还相信我传递了正确的令牌,因为我尝试传递了一个随机令牌,这导致了错误的响应


问题是我将获得UnauthorizedError:未找到授权令牌。

console.log中的err值是多少?console.log(responseJson)?这是期望值。我可以保证这没有什么错