uni-app uni.request报错 TypeError undefined is not an object evaluating e length

uni-app uni.request报错 TypeError undefined is not an object evaluating e length

开发环境 版本号 项目创建方式
Mac 15.5 HBuilderX
### 操作步骤:
服务器返回一个 protobuf 的全部默认值 就会出现,不是全部是默认值就没问题

### 预期结果:
跟 web 环境一样 ,能够在 success 里面正常进入

### 实际结果:
报错了 TypeError: undefined is not an object (evaluating 'e.length')

### bug描述:
uni.request 在  
Content-type=application/x-protobuf  
responseType: 'arraybuffer',  
dataType: 'protobuf',  

的时候; 如果服务器返回的数据是 定义的 protobuf 默认值:  

例如: message Success {
bool success = 1;
}
服务器返回了  Success.success=0 ;  

这个时候,抓包看 服务器返回的 contentLength 是 0  

然后在 web环境正常, IOS 环境下面 uni.request  的 success fail complete 方法都不能进入;控制台报错  TypeError: undefined is not an object (evaluating 'e.length')  

更多关于uni-app uni.request报错 TypeError undefined is not an object evaluating e length的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app uni.request报错 TypeError undefined is not an object evaluating e length的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个错误通常发生在处理空响应数据时。针对protobuf返回默认值的情况,可以尝试以下解决方案:

  1. 检查服务器端返回的Content-Length头是否正确,即使返回空数据也应该有正确的Content-Length: 0

  2. 在uni.request中添加错误处理:

uni.request({
    url: 'your_api_url',
    method: 'POST',
    header: {
        'Content-Type': 'application/x-protobuf'
    },
    responseType: 'arraybuffer',
    dataType: 'protobuf',
    success: (res) => {
        if(res.data && res.data.byteLength > 0){
            // 处理正常数据
        } else {
            // 处理空数据情况
        }
    },
    fail: (err) => {
        console.error('请求失败:', err)
    }
})
回到顶部