【问题标题】:Getting undefined when accessing headers of a request访问请求标头时未定义
【发布时间】:2021-08-24 16:52:19
【问题描述】:

我正在为我的 API 使用 Express JS / Node JS:

这是我的 API 代码:

router = express.Router();

router.post('/someendpoint', async (request, response) =>{
    
    //do some stuff
    
    response.set("accessToken", accessToken);

    response.cookie("accessToken", accessToken, {secure: true, httpOnly: true});
    //do some other stuff and then return data
});

使用 Postman,我可以在标题中看到 cookie 和 accessToken:

这是我的 React JS 代码:

  someFunction = async () =>{
      const email = "someemail@email.com"
      let result = await axios.post(`http://localhost:5000/someendpoint`, {email}, {withCredentials: true});
    
    
       /** some code */
    
      authToken = result.headers["accessToken"];  //getting undefined here
      console.log(authToken);
 }

在 React JS 中,当我访问标头时,我变得不确定,这是什么原因?

当我在“数据”而不是标题中传递 accessToken 时,它正在工作。

authToken = result.data.accessToken;  

【问题讨论】:

标签: javascript node.js reactjs express axios


【解决方案1】:

我不知道您在客户端和版本中使用的是什么,但我建议您正确检查访问结果的标题。 我看到了 nodejs 的版本和模块,你必须使用 result.header.accessToken,甚至 result.headers('accessToken')。

检查结果的结构和/或其中存在哪些函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-10-17
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多