uni-app 点击获取手机号提示TypeError: Cannot read property 'short_name' of undefined错误

uni-app 点击获取手机号提示TypeError: Cannot read property ‘short_name’ of undefined错误

示例代码:

<button class="but_bgm fs14 pad10_tb mar40_b" open-type="getPhoneNumber"
@getphonenumber='$api.getphonenumber($event,1)' :disabled="!is_submit">百度手机号快捷登录</button>

操作步骤:

  • 点击button获取用户手机号报错

预期结果:

  • 点击button获取用户手机号调起授权

实际结果:

  • 点击button获取用户手机号报错

bug描述:

  • 点击获取手机号提示TypeError: Cannot read property ‘short_name’ of undefined错误

| 项目         | 信息          |
|--------------|---------------|
| 产品分类     | uniapp/小程序/百度 |
| PC开发环境操作系统 | Windows       |
| PC开发环境操作系统版本号 | 10            |
| HBuilderX类型   | 正式          |
| HBuilderX版本号 | 3.2.16        |
| 第三方开发者工具版本号 | 3.47.1        |
| 基础库版本号   | 3.300.13      |
| 项目创建方式   | HBuilderX      |

更多关于uni-app 点击获取手机号提示TypeError: Cannot read property 'short_name' of undefined错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 点击获取手机号提示TypeError: Cannot read property 'short_name' of undefined错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个错误通常是因为在百度小程序中获取手机号时,$event.detail 对象的结构与微信小程序不同。

在百度小程序中,@getphonenumber 事件返回的 detail 对象包含 code 字段,但没有微信小程序中的 encryptedDataiv 字段。错误信息中提到的 short_name 属性未定义,很可能是你的 $api.getphonenumber 方法在处理事件参数时,尝试访问了不存在的属性。

解决方案:

  1. 检查事件处理方法: 修改你的 getphonenumber 方法,确保它能兼容百度小程序的参数结构:
// 在 API 方法中
getphonenumber(e, type) {
  // 百度小程序:e.detail.code
  // 微信小程序:e.detail.encryptedData, e.detail.iv
  
  const code = e.detail.code
  if (code) {
    // 百度小程序处理逻辑
    // 将 code 发送到后端,后端通过百度开放平台接口换取手机号
  } else {
    // 微信小程序处理逻辑
    const { encryptedData, iv } = e.detail
    // 其他处理...
  }
}
  1. 确保已正确配置:

    • 在百度开放平台确认已获取 手机号权限
    • manifest.json 中正确配置百度小程序 AppID
    • 确保百度小程序基础库版本支持获取手机号功能
  2. 简化调试: 可以先简化事件处理,直接打印事件对象查看结构:

<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
  获取手机号
</button>
methods: {
  onGetPhoneNumber(e) {
    console.log('事件详情:', JSON.stringify(e.detail))
    // 根据实际结构调整处理逻辑
  }
}
回到顶部