收藏
回答

setdata赋值后为什么this.data显示不出来?


函数最前边曾经 const _this = this这样定义过。

从数据库中取出的数据在res.data[0]中。用如下的方式赋值到OpenId中

_this.setData({

OpenId:res.data[0].OpenId

})

可是:console.log("SS",this.data.OpenId);中却没有值,在控制台的AppData中是有值的啊。

请各位大佬解惑。

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

9 个回答

  • 🇪 🇱 🇸 🇪
    🇪 🇱 🇸 🇪
    06-11

    刚赋值完就打印了,估计值还没有赋上

    06-11
    有用 2
    回复 1
    • ljh
      ljh
      06-11
      对的,是这个原因
      06-11
      回复
  • showms
    showms
    06-11

    你在db.collection('UserInfo')的success中给setData,在success回调函数外面打印this.data.Openid,那肯定是空的。异步问题哥

    06-11
    有用 1
    回复 1
    • ljh
      ljh
      06-11
      明白了。谢谢了啊。
      06-11
      回复
  • 正青春
    正青春
    06-11

    先在data声明,下边在用this.data.OpenId

    Page({
      data: {
       OpenId:null 
      },
    })
    
    06-11
    有用
    回复
  • ljh
    ljh
    06-11

      onShow() {

        console.log("页面显示", app.globalData.OpenId,this.data.id)

        UserTrail("teacherinfo", "咨询师详情", app.globalData.OpenId, "AA")

            //修改页面访问次数

        

        //11

        const _this = this

        const db = wx.cloud.database()

        db.collection('UserInfo').where({

            _id: this.data.id

            //ValidStatus:true

          })

          .get({

            success: function (res) {

              console.log(res.data[0]);

              console.log("自己通过openid取得的信息", res.data);

              _this.setData({

                //UserName:res.data[0].UserName,

                UserInfo: res.data[0],

                imgurl:res.data[0].UserImage,

                ID:res.data[0]._id,

                OpenId:res.data[0].OpenId

              })

              //return res

            },

            fail: err => {

              reject(err)

            }

          })

          var that=this

          console.log("SS"+this.data.OpenId,this.data.imgurl);

        UserTrail("center", "个人中心", app.globalData.OpenId, "CC")

          this.getData()

        //11  

      },


    06-11
    有用
    回复 4
    • hello world
      hello world
      06-11
      你之前没写过js吧,回调里面才能获取到值
      06-11
      回复
    • ljh
      ljh
      06-11回复hello world
      你帮帮我吧。
      06-11
      回复
    • 一笑皆春
      一笑皆春
      06-11
      请求是异步的,还没请求回来就打印了SS,肯定是空的
      06-11
      2
      回复
    • ljh
      ljh
      06-11回复一笑皆春
      是这个原因,谢谢了啊。
      06-11
      回复
  • 一笑皆春
    一笑皆春
    06-11

    setData之前打印一下res.data[0].OpenId是个啥玩意

    06-11
    有用
    回复 7
    • ljh
      ljh
      06-11
      是OpenID。是有数据的
      06-11
      回复
    • 一笑皆春
      一笑皆春
      06-11回复ljh
      那就是console.log("SS",this.data.OpenId);这句话你写的位置不对,得上完整代码了
      06-11
      回复
    • ljh
      ljh
      06-11回复一笑皆春
      我把代码发给您?您给看看?
      06-11
      回复
    • 一笑皆春
      一笑皆春
      06-11回复ljh
      请求是异步的,还没请求回来就打印了SS,肯定是空的,如果不懂什么是异步,网上搜搜多学学
      06-11
      回复
    • 一笑皆春
      一笑皆春
      06-11回复ljh
      onShow() {
          console.log("页面显示", app.globalData.OpenId, this.data.id)
          UserTrail("teacherinfo", "咨询师详情", app.globalData.OpenId, "AA")
          //修改页面访问次数
          //11
          const _this = this
          const db = wx.cloud.database()
          db.collection('UserInfo').where({
            _id: this.data.id
            //ValidStatus:true
           })
           .get({
            success: function(res) {
             console.log(res.data[0]);
             console.log("自己通过openid取得的信息", res.data);
             _this.setData({
              //UserName:res.data[0].UserName,
              UserInfo: res.data[0],
              imgurl: res.data[0].UserImage,
              ID: res.data[0]._id,
              OpenId: res.data[0].OpenId
             })
             //return res
             console.log("SS" + _this.data.OpenId, _this.data.imgurl);
             UserTrail("center", "个人中心", app.globalData.OpenId, "CC")
             _this.getData()
            },
            fail: err => {
             reject(err)
            }
           })
          //11
         },
      06-11
      回复
    查看更多(2)
  • 跨商通
    跨商通
    06-11

    大概率是js异步问题。

    06-11
    有用
    回复
  • hello world
    hello world
    06-11

    this.data.OpenId 为啥不继续使用_this

    06-11
    有用
    回复 1
    • ljh
      ljh
      06-11
      继续使用_this也是没有数据。
      06-11
      回复
  • FeinZee
    FeinZee
    06-11

    光看这段说法看不出问题,有没有可能把page的代码贴上来

    06-11
    有用
    回复 1
    • ljh
      ljh
      06-11
      代码很长,我怎么发给您?
      06-11
      回复
  • Wei
    Wei
    06-11

    请提供完整的代码片段

    06-11
    有用
    回复 2
    • ljh
      ljh
      06-11
      私信发给您试试
      06-11
      回复
    • ljh
      ljh
      06-11
      代码发了,麻烦您给看看?
      06-11
      回复
登录 后发表内容