用户中心bug-无法跳转欢迎页


用户中心项目问题-无法跳转欢迎页



  1. 关键词
  2. welcome
  3. 欢迎页
  4. 用户中心
  5. 无法跳转
  6. 问题描述


在登录后,无法跳转到欢迎页,经过chrome查看login和current接口均有返回值


进一步排查后,发现在登录接口有返回值

// Login/index.tsx文件
const handleSubmit = async (values: API.LoginParams) => {
   try {
     // 登录
     const user = await login({
       ...values,
       type,
    });
     // 此处user有值!!!!!
     if (user) {
       const defaultLoginSuccessMessage = '登录成功!';
       message.success(defaultLoginSuccessMessage);
       await fetchUserInfo();
       /** 此方法会跳转到 redirect 参数所在的位置 */
       if (!history) return;
       const { query } = history.location;
       const { redirect } = query as {
         redirect: string;
      };
       history.push(redirect || '/');
       return;
    }


然后if代码块中调用的await fetchUserInfo();的方法中currentUser为空,所以InitialState中没有封装currentUser

// Login/index.tsx文件
const fetchUserInfo = async () => {
   const currentUser = await initialState?.fetchUserInfo?.();
   if (currentUser) {
     await setInitialState((s) => ({
       ...s,
       currentUser
    }));
  }
};


导致在app.tsx中,页面更新时,也就是登陆后跳转页面时,查询不到登录用户的信息,不断跳转到登录页

onPageChange: () => {
     const { location } = history;
     if (NO_NEED_LOGIN_WHITE_LIST.includes(location.pathname)) {
       return;
    }
     // 如果没有登录,重定向到 login
     // 此处currentUser为空,所以导致一直重定向
     if (!initialState?.currentUser) {
       history.push(loginPath);
    }
  },


而在chrome开发工具中开current的请求是有返回数据的,不明白为什么currentUser中没有封装上数据


  1. 解决


上面的问题描述中说了,原因是currentUser没有封装有数据,所以更改了fetchUserInfo->queryCurrentUser方法中的返回结果泛型

修改前(apt.tsx文件):

export async function currentUser(options?: { [key: string]: any }) {
 return request<{
   data: API.CurrentUser
}>('/user/current', {
   method: 'GET',
   ...(options || {}),
});
}


修改后(apt.tsx文件):

export async function currentUser(options?: { [key: string]: any }) {
 return request<
   API.CurrentUser
 >('/user/current', {
   method: 'GET',
   ...(options || {}),
});
}


总结一下原因,因为后端返回值是一个对象,而前端的封装层级比后端传回来的多一级,所以封装不上数据,前后端的对象容器不一样,所以内容放不进去


修改了上面的返回值类型后,还需修改在app.tsx中的fetchUserInfo方法定义

修改前:

const fetchUserInfo = async () => {
   try {
      const msg = await queryCurrentUser();
      return msg.data;
  } catch (error) {
     history.push(loginPath);
  }
   return undefined;
};


修改后:

const fetchUserInfo = async () => {
   try {
      return await queryCurrentUser();
  } catch (error) {
     history.push(loginPath);
  }
   return undefined;
};


0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
plaing
作者分享
#自我介绍# 大家好,我是幻梦。本科双非,目前在读研究生。本省四年,零零散散学习的Java的许多知识,自己感觉没有把知识系统的练习起来,也没有做项目,做项目的时候下不去手,也没有自己的idea,不知道做什么项目,对于自己没有掌握的知识比较恐惧应用的项目中去,目前在向很多人学习,望在未来能克服自己对未知的恐惧,做一个更好的自己。
4
Jenkins自动化部署-教程
36
day5 今日 - 域名备案✓ * 提交初审 * 博客文章详情✓ * 当前后端传递Long数据精度丢失时,可在字段上添加@JsonSerialize(using = ToStringSerializer.class) 注解 * 即时聊天✕ * [ 最佳买卖股票时机含冷冻期](https://www.programmercarl.com/0309.最佳买卖股票时机含冷冻期.html#_309-最佳买卖股票时机含冷冻期)✓ - [动规周总结](https://www.programmercarl.com/周总结/20210304动规周末总结.html) ✓ * [买卖股票的最佳时机含手续费](https://www.programmercarl.com/0714.买卖股票的最佳时机含手续费(动态规划).html#_714-买卖股票的最佳时机含手续费)✓ * java技术核心卷20页✓ * 女孩别怕一章✓ * 牛客50题✓ * 用户中心末✕ * [mysql](https://www.bilibili.com/video/BV1Vy4y1z7EX)✓ * 跑步 明日 - [使用线程池更新阅读次数](https://www.bilibili.com/video/BV1Gb4y1d7zb?p=19&spm_id_from=pageDriver) * [ mysql](https://www.bilibili.com/video/BV1Vy4y1z7EX?p=22&spm_id_from=pageDriver) * [最长递增子序列](https://www.programmercarl.com/0300.最长上升子序列.html#_300-最长递增子序列) * [最长连续递增序列](https://www.programmercarl.com/0674.最长连续递增序列.html#_674-最长连续递增序列) * java技术核心卷20页 * 女孩别怕一章 * 用户中心末 * 即时聊天
13
day4 今日 * 牛客50题✓ * 女孩别怕一章✓ * 博客登录拦截器✓ * cdn加速✓ - gitlab服务器搭建直播回放下✓ * 前端监控系统 * 作用 * 监控系统正常运行 * 帮助排除bug * 帮助感知问题,防止用户流失 * * java技术核心卷20页✓ * [买卖股票的最佳时机III](https://www.programmercarl.com/0123.买卖股票的最佳时机III.html#_123-买卖股票的最佳时机iii)✓ * [买卖股票的最佳时机IV](https://www.programmercarl.com/0188.买卖股票的最佳时机IV.html#_188-买卖股票的最佳时机iv)✓ * 收藏列表返回值错误✓ * 分页总数返回错误✓ * 通过mapper查询到的实体对象,要和放到PageInfo里的一致才能正确显示记录总数 明日 - 域名备案 * 博客文章详情 * 即时聊天 * [ 最佳买卖股票时机含冷冻期](https://www.programmercarl.com/0309.最佳买卖股票时机含冷冻期.html#_309-最佳买卖股票时机含冷冻期) - [动规周总结](https://www.programmercarl.com/周总结/20210304动规周末总结.html)  * [买卖股票的最佳时机含手续费](https://www.programmercarl.com/0714.买卖股票的最佳时机含手续费(动态规划).html#_714-买卖股票的最佳时机含手续费) * java技术核心卷20页 * 女孩别怕一章 * 牛客50题 * 用户中心末 * [mysql](https://www.bilibili.com/video/BV1Vy4y1z7EX)
17
day3 今日 - [ 买卖股票的最佳时机](https://www.programmercarl.com/0121.买卖股票的最佳时机.html#_121-买卖股票的最佳时机)✓ - [ 买卖股票的最佳时机II](https://www.programmercarl.com/0122.买卖股票的最佳时机II(动态规划).html#_122-买卖股票的最佳时机ii)✓ * 牛客50题✓ * 女孩别怕一章✓ * 码神博客登录功能✓ * 博客注册✓ * java技术核心卷20页✓ * 服务器搭建直播回放上✓ * CDN加速✕ * 开题报告✓ * 放开获取用户发布帖子列表接口✓ 明日 * 牛客50题 * 女孩别怕一章 * 博客登录拦截器 * cdn加速 * 域名备案 * gitlab服务器搭建直播回放下 * java技术核心卷20页 * 算法题两道 我记得发了day2的呀[尴尬]
14
下载 APP