收藏
回答

页面中globalData为null?

做了个登录页面,在页面中用globalData时为null,数据库里面有信心,每次刷新都要重新登录,这个问题怎么解决,求大师帮帮忙。

me.js代码
const app = getApp();
// const db = wx.cloud.database();
Page({
    /**
     * 页面的初始数据
     */
    data: {    
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) { 
        console.log(app.globalData.userInfo)
        this.setData({
         userInfo:app.globalData.userInfo
     })
    },
    // 获取头像
    getAvatar(event){
       console.log(event.detail.avatarUrl);//临时头像路劲地址
       let avatarUrl = event.detail.avatarUrl;//临时地址
       let suffix = /\.[^\.]+$/.exec(avatarUrl)[0];
       wx.cloud.uploadFile({
           cloudPath: 'loginImages/' + new Date().getTime() + suffix,
           filePath:avatarUrl  //上传到云端的路劲,
       }).then(res => {
           console.log(res.fileID);
           this.setData({
               avatarUrl:res.fileID
           })
       })
    },
    login(event){
        console.log(event.detail.value.nickName);
        let nickName =event.detail.value.nickName;
        if(!nickName){
            wx.showToast({
                title:'请填写昵称',
                icon:'error'
            })
            return
        }
        if(!this.data.avatarUrl){
            wx.showToast({
                title:'请上传头像',
                icon:'error'
            })
            return
        }
        var that = this;
        console.log(app.globalData.openid);
        wx.cloud.database().collection('login_users').where({
            _openid:app.globalData.openid
        }).get({
            success(res){
                console.log(res)
                if(res.data.length == 0){
                    //添加记录到数据库
                    wx.cloud.database().collection('login_users').add({
                        data:{
                            num:Date.now(),//!QQ号
                            avatarUrl:that.data.avatarUrl,
                            nickName:nickName
                        },
                        success(res){
                            console.log(res)
                            wx.showToast({
                                title:'登陆成功',
                            })
                            //设置userInfo这个对象
                            let userInfo = {};
                            userInfo.avatarUrl = that.data.avatarUrl;
                            userInfo.nickName = nickName;
                            that.setData({
                                userInfo
                            })
                            // //第一次登录成功后获取最新用户信息
                            that.getUseInfo()
                        }
                    })
                }else{
                    that.setData({
                        userInfo:res.data[0]
                    })
                }
            }  
        })
    },
    loginOut(){
        // console.log(event);
        app.globalData.userInfo = null
        this.setData({
            userInfo:null
        })
    },
    //修改个人信息
    editUserinfo(){
       wx.navigateTo({
           url:'edit/edit'
       })
    },
     /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log(app.globalData.userInfo);
        this.setData({
            userInfo:app.globalData.userInfo
        }) 
       
    },
  
    //获取用户信息
    getUseInfo(){
        let that = this;
        wx.cloud.database().collection('login_users').where({
            _openid:app.globalData.openid
          }).get({
            success(result){
              console.log(result)
              app.globalData.userInfo = result.data[0]
            }
          })
    }
})

aapp.js代码
App({
  onLaunch: function () {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力');
    } else {
      wx.cloud.init({
        env:'',
        traceUser: true,
      });
    }
    //获取用户的openid
    var that = this;
    wx.cloud.callFunction({
      name:'getOpenid',
      success(res){
        console.log(res);
        that.globalData.openid = res.result.openid
        //查找数据库用户表里面是否有这个用户记录
        wx.cloud.database().collection('login_users').where({
          _openid:res.result.openid
        }).get({
          success(result){
            console.log(result)
            that.globalData.userInfo = result.data[0]
          }
        })
      }
    })
},
 globalData:{
    userInfo:null,
    openid:null
  }
});


回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    04-03

    异步问题,先取值后赋值

    04-03
    有用
    回复 18
    • 蓝天☁
      蓝天☁
      06-05
      代码要怎么改呢?
      06-05
      回复
    • Mr.Zhao
      Mr.Zhao
      发表于移动端
      06-05回复蓝天☁
      过去两个月了,还不知道怎么改?
      06-05
      回复
    • 蓝天☁
      蓝天☁
      06-05
      忙了段时间,没有弄这个,关于异步这个问题,查了很多,没有实际的案例,有点搞不懂,下面找了些资料写的代码,没有效果。另外试了这种方法,if(that.userInfoCallback){
        that.userInfoCallback(result.data[0]),授权之后要重新编译一下,才获取到。这种方式还有个问题就是,没有授权之前,用户信息就会报错,undefined,比如用户头像,用户昵称
      06-05
      回复
    • Mr.Zhao
      Mr.Zhao
      06-05回复蓝天☁
      写的很有问题,get不加where条件获取第一条不就串号了。用云开发为啥还要获取openid?
      06-05
      回复
    • 蓝天☁
      蓝天☁
      06-05回复Mr.Zhao
      是不是该成这样?但是还是一样的,这个代码片段是全部的代码
      https://developers.weixin.qq.com/s/kffn9Jmc7HR5
      06-05
      回复
    查看更多(13)
登录 后发表内容