收藏
回答

报错 Error: Expected updated data but...

小程序报错

Expected updated data but get first rendering data;Expected updated data but get first rendering data

Error: Expected updated data but get first rendering data

在开发工具上会二次渲染,但是在手机上就不能了,而且这个报错是时有时无的,求大神分享一下报错的原因,该如何排查呢


在网上也有人遇见同样的问题了


解决方案:在app.js中还没有给globalData赋值时却提前跳转到了调用globalData数据的页面,所以导致渲染失败,个人建议做个引导或加载页面,给数据一个缓冲的过程;


我在App.js里的代码

App({
   onLaunch: function () {
     this .getOpenid().then(()=>{
         return that.setAdmin()
     })
   },
   getOpenid: function () {
       var that = this
       return new Promise( function (resolve, reject) {
           wx.getStorage({
               key: 'openid' ,
               success: function (res) {
                  that.globalData.openId = res.data
                  return resolve( 'app.js login success' )
               },
               fail: function () {
                   wx.login({
                       success: res => {
                           var code = res.code; //返回code
                           var appId =
                           var secret =
                           wx.request({
                               url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code' ,
                               data: {},
                               header: {
                                   'content-type' : 'json'
                               },
                               success: function (res) {
                                   wx.setStorage({
                                       key: "openid" ,
                                       data: res.data.openid
                                   })
                                   that.globalData.openId = res.data.openid
                                   return resolve( 'app.js login success' )
  
                               }
                           })
                       }
                   })
               }
           })
       })
   },
   setAdmin: function () {
       var that = this
       return new Promise( function (resolve, reject) {
           wx.request({
               url: 'http://132.232.22.140:8889/api/club/adminComfirm' ,
               method: 'post' ,
               data:{
                   id:that.globalData.openId
               },
               header:{
                   "content-type" : 'application/json'
               },
               success: function (res){
                   if (res.data.code ==300){
                       wx.reLaunch({
                           url: '../../pages/findpage/index' ,
                       })
                   }
                     if (res.data.code == 200){
                         that.globalData.myclub = res.data.clubnumber
                         that.globalData.myname = res.data.name
                         that.globalData.adminOn = true
                         wx.reLaunch({
                             url: '../../pages/index/index' ,
                         })
                     }
               }
           })
       })
   },
   globalData: {
     userInfo: null ,
     openId: null ,
     myclub: null ,
     myname: null ,
     adminOn: false ,
     findClub: null ,
     findClubNumber: null
   }
})

因为之前就遇见过页面初始化完成之后没有得到数据,所以我在首页设置的是一个空页面

app.json

"pages" : [
         "pages/midware/index" ,
         "pages/index/index" ,
         "pages/findpage/index" ,
         "pages/signup/index" ,
         "pages/join/index" ,
         "pages/option/index" ,
         "pages/details/index" ,
         "pages/setting/index" ,
         "pages/memo/index"
     ],

第一个就是空页面,然后主页面的选择在上面App.js里面来判断的,按道理来说,在页面初始化过程中除了对globalData进行赋值之外没有任何取值的操作呀

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

1 个回答

  • Jimmy, Ann
    Jimmy, Ann
    2018-08-01

    onLaunch  要比pages/midware/index onLoad 执行的要慢 所以赋值也会慢一点 会造成赋不了值的情况出现

    2018-08-01
    有用
    回复 6
    • 爱死三土啦
      爱死三土啦
      2018-08-01

      谢谢前辈解答,但是 页面Onload里面没有任何方法呀,没有对全局进行赋值或是取值

      2018-08-01
      回复
    • Jimmy, Ann
      Jimmy, Ann
      2018-08-01

      getApp().globalData.openId 动态取值 最好外加判断是否有值的情况

      2018-08-01
      回复
    • 爱死三土啦
      爱死三土啦
      2018-08-01回复Jimmy, Ann

      谢谢前辈再次解答,动态取值能稍微聚一下例子吗,想了一下脑子里想不到这个要怎么做

      2018-08-01
      回复
    • Jimmy, Ann
      Jimmy, Ann
      2018-08-02

      app.js 的globaData.openId =5   在 B 页面的js里面 onLoad 或者onShow 里面用 一个变量接受 var dataID=getApp(). globalData openId 如果想在View 层显示 就 this.setData({ aa: dataID})

      2018-08-02
      回复
    • 爱死三土啦
      爱死三土啦
      2018-08-02回复Jimmy, Ann

      谢谢大佬解答,我又在网上找了一些资料看,但是还是有一事想不通,因为即使


      onLaunch  要比 onload要慢,但是onload里面没有任何方法,甚至整个index页面没有任何方法呀


      2018-08-02
      回复
    查看更多(1)
登录 后发表内容