You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-Native中fetch二次调用丢失NodeJS后端会话问题求助

React-Native中Fetch调用会话接口的异常行为

我的应用启动后,首要任务是携带access token调用POST接口/handshake完成授权。Node后端逻辑如下:检查会话变量req.session.uid是否已设置,若未设置则验证请求体中的access token,验证通过后设置req.session.uid。

在Postman中测试完全正常:成功调用/handshake后,后续所有API请求都能正常使用req.session.uid。但在React-Native中使用fetch时,当会话已存在会出现异常:

场景一:无会话存在

  • 携带正确access token调用/handshake → 后端设置req.session.uid
  • 后续所有API调用均可正常使用已设置的uid

场景二:会话已存在

  • 携带正确access token调用/handshake → 因req.session.uid已存在,后端不修改该变量
  • 发起其他API调用 → req.session.id为undefined

为排查问题,我在首次/handshake成功后再次调用该接口:

  • 第一次调用:携带正确access token,后端因req.session.uid已存在,不修改变量
  • 第二次调用:携带正确access token,后端发现req.session.uid变为undefined,重新设置该变量

第二次调用/handshake后,所有后续API调用均恢复正常。

核心疑问:为何后端在首次/handshake调用中能识别会话,二次调用时却无法识别?

以下是React-Native中调用/handshake的代码:

fetch(this.Links.handshake, 
    {body: myBody, method:"POST", credentials: "include",
    headers:{ 'Content-Type' : 'application/x-www-form-urlencoded'}})
.then((response) => {
    if(response.ok)
        console.log("Handshake was successfull");
    else
        console.warn("Handshake failed ("+response.status+")!");
}).catch(() => {
    console.warn("Error happened while handshake...");
})

内容的提问来源于stack exchange,提问作者Sercan Samet Savran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:40:13