The problem of Global variable change in APPjs

wx.getSetting({
  success: res => {
    if (res.authSetting["scope.userInfo"]) {
      wx.getUserInfo({
        success: res => {         
          this.globalData.userInfo = res.userInfo
        }
      })
    } else (
      wx.authorize({
        scope: "scope.userInfo",
        success() {
          wx.getUserInfo({
            success:res=>{
               this.globalData.userInfo =res.userInfo
            }
          })
        }
      })
    )
  }
})

console.log("wa")
this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })

I console for a while and find that onload will have "wa"," when the authorization application pops up in the debug interface. But the life cycle onlunch of Mini Program seen on the Internet is higher than onload. According to reason, I changed the global userInfo variable after the authorization was successful. When loading the page, the local variable of the page can also successfully obtain the global global variable, but it is still null

in the console.

use this.userInfoReadyCallback in the official example to solve the problem, but this.userInfoReadyCallback does not seem to be found in the official API, and the loading order of the page is still in doubt. Ask God to explain ~

Mar.06,2021

1. GetUserInfo is a network request, and callback may not be performed until after onLoad.
2. UserInfoReadyCallback is a custom method that adds a userInfoReadyCallback method to App if the getUserInfo of the App instance returns after onLoad. When the getUserInfo callback returns, it is executed.

Menu