如何修复导致未定义错误的属性“状态”的 Axios 拦截器

时间:2021-06-17 20:51:55

标签: vue.js axios

我可以选择将元素的权限设置为全局或私有。我正在使用 Axios 拦截器请求来处理查找权限字段以获取数据,如果有,则对其进行字符串化。问题是,当我尝试重新加载程序时,它会导致我收到“类型错误:无法读取未定义的属性‘状态’”。现在唯一的“修复”是注销,移除拦截器,登录,阅读,然后再次运行检查。

因此,我什至无法进入该软件的主页仪表板。如果我清除了 cookie,我可以返回登录屏幕,但在尝试登录后不能再返回。

有什么我想念的吗?下面是拦截器代码。如果需要更多信息或上下文,请告诉我。

export default {
  install: (Vue) => {
    Vue.$eventBus = new Vue();

    Vue.axios.interceptors.response.use(response => {
      return response.data;
    }, async error => {
      if (error.response.status === 401 && error.config.url != '/api/authentication/login') {
        var config = await Vue.$configService.find();
        window.location = config.accountPortalUrl;
        return
      }      
      console.log(error);
      Vue.$eventBus.$emit('notifyUser', 'Uh oh, something went wrong!');
      return Promise.reject(error);
    });

    Vue.axios.interceptors.request.use(
      config => {
        // check request method -> use post if many params
        if (config.data.permissions) {
          config.data.permissions = JSON.stringify(config.data.permissions);
        }
        console.log(config);
        return config;
       
      }
    );
  }
};

2 个答案:

答案 0 :(得分:1)

您的服务 API 似乎没有响应,如果用户未通过身份验证,则可能会发生这种情况。您的错误位于您检查的行(error.response.status)。当请求在响应之前被中断时,它只能得到一个未定义的响应。很可能如果您检查浏览器网络面板,您会看到此请求的预检检查导致 401 网络错误。因此,由于预检失败,您的实际响应未定义。您应该首先检查您的服务器是否响应响应,然后访问响应状态。

这样的事情可能会有所帮助

Sub UpdateFiles()
    Dim rownum As Long
    rownum = 2
    
    Dim last_row As Long
    last_row = Cells(Rows.Count, 1).End(xlUp).Row
    
    Dim filename As String
    Dim newvalue As Long
    
    For nextRow = 2 To last_row
        filename = Range("A" & rownum).Value     
        newvalue = ActiveWorkbook.Sheets("Sheet1").Range("B" & rownum).Value
    
        Workbooks.Open "C:\Folder\" & filename        
        ActiveWorkbook.Unprotect ("password")        
        ActiveWorkbook.Sheets("References").Range("F2") = newvalue        
        ActiveWorkbook.Protect ("password")        
        ActiveWorkbook.Save        
        ActiveWorkbook.Close
    
        rownum = rownum + 1
    Next nextRow
End Sub

答案 1 :(得分:0)

所以,答案最终非常简单。

if (config.data.permissions)

必须

if (config.data && config.data.permissions)