收藏
回答

data对象取值为空

问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
API和组件 小程序 Bug data 客户端 6.5.3 2.6.1

- 当前 Bug 的表现(可附上截图)



这个原因是因为为checked赋值的方法,和读方法 不是同步的.






把延迟去掉就得不到值, 疑问就在这, 为啥data可以打印出来, 但是data中的值就取不出来

















最后一次编辑于  03-07
回答关注问题邀请回答
收藏

1 个回答

  • Smile
    Smile
    03-07

    请问你是设置checked的值时候和取值的时候的函数的顺序是什么,js的引用类型出现你第一张图的情况没什么奇怪的,能把代码全吗?

    03-07
    赞同
    回复 12
    • 兴
      03-07


      // 代码我给整理一下, 大致的逻辑是这样的

      Page({

      /**

        * 页面的初始数据

        */

      data: {

      index: 2,

      background: ['监控', '数据', '开关'],

      indicatorDots: true,

      vertical: false,

      checked: [],

      },

      Tag: '0%',

      tempt: [],

      sld: [],

      lastdata: [],

      eid: '', //查询土温

      weaeid: '', //查询气象

      url: ''

      },

      /**

        * 生命周期函数--监听页面加载

        */

      onLoad: function (option) {

      var that = this

      that.setData({

      url: app.crrUrl + '/static/login/back31.jpg'

      })

      var that = this

      that.setData({

      id: option.id

      });

      //that.getEquData();

      that.selEquip(option.id);  //


      } ,

      //根据小区ID,查询最后一次的采集数据  

      getEquData: function (id) {

      var that = this

      var sld = that.data.sld

      wx.request({

      url: app.crrUrl + '/mwx/getEquData.do',

      header: {

      "content-Type": "application/x-www-form-urlencoded"

      },

      method: "POST",

      data: {

      unitId: id

      },

      success: function (res) {

      var conf = res.data.pd.equ_config;

      var dtoconf = that.data.configDtos

      var checked = []

      var confBoo = typeof (dtoconf) != "undefined" && typeof (conf) != "undefined"

      if (confBoo) {

      for (var i = 0; i < dtoconf.length; i++) {

      var check = []

      var dcont = dtoconf[i].controls

      for (var k = 0; k < dcont.length; k++) {

      for (var j = 0; j < conf.length; j++) {

      if (conf[j].controlId == dcont[k].id) {

      if (conf[j].controlStatus == "开启") {

      check[k] = true;

      } else {

      check[k] = false;

      }

      }

      }

      }

      checked[i] = check

      }

      }

      console.log(123456, '看到回复就')

      that.setData({

      equ_fertilize: equ_fertilize,

      equ_weather: equ_weather,

      mal: mal,

      Ahum: Ahum,

      checked: checked,

      time: time,

      lastdata: synthesize,


      });

      wx.hideLoading()

      }

      })

      },

      selEquip: function (id) {

      //根据小区编号,查询小区下的节点信息   f

      var that = this

      setTimeout(function () {

      console.log('date1: ', that.data)

      console.log('date2: ', that.data.checked)

      }, 100);




      wx.request({

      url: app.crrUrl + '/mwx/selEquip.do',

      header: {

      "content-Type": "application/x-www-form-urlencoded"

      },

      method: "POST",

      data: {

      unitId: id

      },

      success: function (res) {


      var coll = (typeof (res.data.collectDtos) == 'undefined') ? [] : res.data.collectDtos;

      var weat = (typeof (res.data.weatherDtos) == 'undefined') ? [] : res.data.weatherDtos;

      var fer = (typeof (res.data.fertilizeDtos) == 'undefined') ? [] : res.data.fertilizeDtos;

      var conf = (typeof (res.data.configDtos) == 'undefined') ? [] : res.data.configDtos;

      var mon = (typeof (res.data.monitorDtos) == 'undefined') ? [] : res.data.monitorDtos;

      var eid = res.data.weatherDtos[0].equipment.id;



      var equip = '';//防止土壤数据为空时前台报错


      that.setData({

      collectDtos: coll,

      weatherDtos: weat,

      fertilizeDtos: fer,

      configDtos: conf,

      monitorDtos: mon,

      monmsg: mon.length == 0,

      confmsg: conf.length == 0,

      masage: fer.length == 0,

      sld: r,

      eid: equip,

      weaeid: eid

      });

      that.getEquData(id) //得到实时数据

      }

      })

      }

      });


      03-07
      回复
    • 兴
      03-07

      // 代码的逻辑又重新整理一下

      Page({

      /**

        * 页面的初始数据

        */

      data: {

      index: 2,

      background: ['监控', '数据', '开关'],

      indicatorDots: true,

      vertical: false,

      checked: [],

      },

      /**

        * 生命周期函数--监听页面加载

        */

      onLoad: function (option) {

      var that = this

      that.setData({

      url: app.crrUrl + '/static/login/back31.jpg'

      })

      var that = this

      that.setData({

      id: option.id

      });

      //that.getEquData();

      that.selEquip(option.id);  //


      } ,

      //根据小区ID,查询最后一次的采集数据  

      getEquData: function (id) {

      var that = this

      var sld = that.data.sld

      wx.request({

      url: app.crrUrl + '/mwx/getEquData.do',

      header: {

      "content-Type": "application/x-www-form-urlencoded"

      },

      method: "POST",

      data: {

      unitId: id

      },

      success: function (res) {

      var conf = res.data.pd.equ_config;

      var dtoconf = that.data.configDtos

      var checked = []

      var confBoo = typeof (dtoconf) != "undefined" && typeof (conf) != "undefined"

      if (confBoo) {

      for (var i = 0; i < dtoconf.length; i++) {

      var check = []

      var dcont = dtoconf[i].controls

      for (var k = 0; k < dcont.length; k++) {

      for (var j = 0; j < conf.length; j++) {

      if (conf[j].controlId == dcont[k].id) {

      if (conf[j].controlStatus == "开启") {

      check[k] = true;

      } else {

      check[k] = false;

      }

      }

      }

      }

      checked[i] = check

      }

      }

      console.log(123456, '看到回复就')

      that.setData({

      equ_fertilize: equ_fertilize,

      equ_weather: equ_weather,

      mal: mal,

      Ahum: Ahum,

      checked: checked,

      });

      wx.hideLoading()

      }

      })

      },

      selEquip: function (id) {

      //根据小区编号,查询小区下的节点信息   f

      var that = this

      setTimeout(function () {

      console.log('date1: ', that.data)

      console.log('date2: ', that.data.checked)

      }, 100);


      wx.request({

      url: app.crrUrl + '/mwx/selEquip.do',

      header: {

      "content-Type": "application/x-www-form-urlencoded"

      },

      method: "POST",

      data: {

      unitId: id

      },

      success: function (res) {

      that.getEquData(id) //得到实时数据

      }

      })

      }

      });


      03-07
      回复
    • Smile
      Smile
      03-07回复

      你先输出checked的值,然后再去下次请求设置checked的值不是很正常吗?你设置checked值之后回调函数输出值就有值了(前提你返回的checked有值)

      this.setData({ checked: checked },  () => {

          console.log(this.data.checked);

      });

      03-07
      回复
    • 兴
      03-07回复Smile

      现在的问题是这个checked还没有通过setData给他设值, 但是能够从this.data中打印出来, 这是不是缓存的原因,还是其他的, 这个搞不太懂

      03-07
      回复
    • Smile
      Smile
      03-07回复

      你自己去了解js引用类型吧,而且我上面已经回复你为什么会这样了

      03-07
      回复
    查看更多(7)