令牌在前端是正确的,但在后端它是未定义的

Token is correct in frontend, but in the backend it's undefined

在 React 前端,我想获取与登录用户关联的交易,其令牌存储在 Redux 中 userLogin 状态变量的 userInfo 属性 中。所以在与获取交易相关的动作创建者函数中,我有以下内容


const token = `Bearer ${getState().userLogin.userInfo.token}`;

console.log(token);

const config = {
  headers: {
    Authorization: token,
  },
};

const { data } = await axios.get('/api/v1/transactions', {}, config);

console.log(toke) 正确输出令牌。

然而,在后端,当我检查 req.headers.authorization 时,我得到 undefined,并且我获取数据的请求因授权错误而被拒绝。为什么在后端 req.headers.authorization=undefined,虽然我在 headers 中从前端发送它?

您传递的配置参数顺序错误,应该是

axios.get('/api/v1/transactions', config);

有关详细信息,请参阅此内容: