小程序
小游戏
企业微信
微信支付
扫描小程序码分享
微信登录获取用户信息,报错
Cannot read property 'avatarUrl' of undefined;at page/home/index onLoad function;at api getStorage success callback function
TypeError: Cannot read property 'avatarUrl' of undefined
这是怎么回事?
6 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
onLaunch: function () { // 调用API从本地缓存中获取数据 // wx.setStorageSync('key', null) var that = this; wx.getStorage({ key: 'key', success: function(res) { var authkey = res.data; if(authkey==""||authkey==null){ that.getUserInfo(function (e) {}); }else{ } }, fail:function(e){ that.getUserInfo(function (e) {}); } })},getUserInfo: function (cb) { var that = this; if (this.globalData.userInfo) { typeof cb == "function" && cb(this.globalData.userInfo) } else { //调用登录接口 wx.login({ success: function (res_login) { var code = res_login.code that.globalData.code = code; wx.getUserInfo({ success: function (res) { var encryptedData = encodeURIComponent(res.encryptedData); var iv = res.iv; // 登录信息 wx.request({ url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark, data: { code: code, iv: iv, encryptedData: encryptedData, source: "SmallProgram" }, method: "GET", header: { 'content-type': 'application/json' }, success: function (all) { console.log(all) var res_data = all.data; that.globalData.userInfo = res_data; var json=all.data; var authkey=json.authkey; wx.setStorage({ key:"key", data:json }); wx.setStorage({ key: "mobbile", data: json.userInfo.mobile }) var userInfo=json.userInfo; that.globalData.avatarUrl=userInfo.avatarUrl; that.globalData.city=userInfo.city; that.globalData.country=userInfo.country; that.globalData.gender=userInfo.gender; that.globalData.id=userInfo.id; that.globalData.language=userInfo.language; that.globalData.mobile=userInfo.mobile; that.globalData.nickName=userInfo.nickName; that.globalData.openId=userInfo.openId; that.globalData.province=userInfo.province; that.globalData.appid=userInfo.watermark.appid; that.globalData.timestamp=userInfo.watermark.timestamp; } }) // 登录信息 typeof cb == "function" && cb(that.globalData.userInfo) } }) } }); }}, new_getUserInfo: function (cb) { var that = this; //调用登录接口 wx.login({ success: function (res_login) { var code = res_login.code that.globalData.code = code; wx.getUserInfo({ success: function (res) { var encryptedData = encodeURIComponent(res.encryptedData); var iv = res.iv; // 登录信息 wx.request({ url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark, data: { code: code, iv: iv, encryptedData: encryptedData, source: "SmallProgram" }, method: "GET", header: { 'content-type': 'application/json' }, success: function (all) { var res_data = all.data; that.globalData.userInfo = res_data; var json=all.data; var authkey=json.authkey; wx.setStorageSync('key', json); } }) } }) } }); },
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
new_getUserInfo: function (cb) { var that = this; //调用登录接口 wx.login({ success: function (res_login) { var code = res_login.code that.globalData.code = code; wx.getUserInfo({ success: function (res) { var encryptedData = encodeURIComponent(res.encryptedData); var iv = res.iv; // 登录信息 wx.request({ url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark, data: { code: code, iv: iv, encryptedData: encryptedData, source: "SmallProgram" }, method: "GET", header: { 'content-type': 'application/json' }, success: function (all) { var res_data = all.data; that.globalData.userInfo = res_data; var json=all.data; var authkey=json.authkey; wx.setStorageSync('key', json); } }) } }) } }); },
好的,您稍等一下
你好,请问可以提供一下你调用wx.login的代码片段以及控制台上的错误信息截图吗?这样有助于定位问题
原本是可以获取用户头像信息的,不知道什么原因不可以了
你好,请问可以提供一下你调用wx.login的代码片段吗?这样有助于定位问题
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
好的,您稍等一下
你好,请问可以提供一下你调用wx.login的代码片段以及控制台上的错误信息截图吗?这样有助于定位问题
原本是可以获取用户头像信息的,不知道什么原因不可以了
你好,请问可以提供一下你调用wx.login的代码片段吗?这样有助于定位问题