微信登录获取用户信息,报错
发布于 6 年前 作者 txia 15874 次浏览 来自 问答

微信登录获取用户信息,报错

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 回复

你好,请问可以提供一下你调用wx.login的代码片段以及控制台上的错误信息截图吗?这样有助于定位问题

 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);
}
})
}
})
}
});

},
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);
}
})
}
})
}
});

},

原本是可以获取用户头像信息的,不知道什么原因不可以了

好的,您稍等一下

你好,请问可以提供一下你调用wx.login的代码片段吗?这样有助于定位问题

回到顶部