收藏
回答

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

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

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 个回答

  • 江南初见
    江南初见
    2017-08-10
    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);
    }
    })
    }
    })
    }
    });

    },


    2017-08-10
    有用
    回复
  • 江南初见
    江南初见
    2017-08-10
     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);
    }
    })
    }
    })
    }
    });

    },



    2017-08-10
    有用
    回复
  • 江南初见
    江南初见
    2017-08-10

    好的,您稍等一下


    2017-08-10
    有用
    回复
  • 韬々
    韬々
    2017-08-10

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

    2017-08-10
    有用
    回复
  • 江南初见
    江南初见
    2017-08-10

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


    2017-08-10
    有用
    回复
  • 韬々
    韬々
    2017-08-10

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

    2017-08-10
    有用
    回复
登录 后发表内容