小程序
小游戏
企业微信
微信支付
扫描小程序码分享
- 当前 Bug 的表现(可附上截图)
这个原因是因为为checked赋值的方法,和读方法 不是同步的.
把延迟去掉就得不到值, 疑问就在这, 为啥data可以打印出来, 但是data中的值就取不出来
1 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
请问你是设置checked的值时候和取值的时候的函数的顺序是什么,js的引用类型出现你第一张图的情况没什么奇怪的,能把代码全吗?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
// 代码我给整理一下, 大致的逻辑是这样的
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'
})
id: option.id
});
//that.getEquData();
that.selEquip(option.id); //
} ,
//根据小区ID,查询最后一次的采集数据
getEquData: function (id) {
var sld = that.data.sld
wx.request({
url: app.crrUrl + '/mwx/getEquData.do',
header: {
"content-Type": "application/x-www-form-urlencoded"
method: "POST",
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, '看到回复就')
equ_fertilize: equ_fertilize,
equ_weather: equ_weather,
mal: mal,
Ahum: Ahum,
checked: checked,
time: time,
lastdata: synthesize,
wx.hideLoading()
selEquip: function (id) {
//根据小区编号,查询小区下的节点信息 f
setTimeout(function () {
console.log('date1: ', that.data)
console.log('date2: ', that.data.checked)
}, 100);
url: app.crrUrl + '/mwx/selEquip.do',
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 = '';//防止土壤数据为空时前台报错
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) //得到实时数据
// 代码的逻辑又重新整理一下
你先输出checked的值,然后再去下次请求设置checked的值不是很正常吗?你设置checked值之后回调函数输出值就有值了(前提你返回的checked有值)
this.setData({ checked: checked }, () => {
console.log(this.data.checked);
现在的问题是这个checked还没有通过setData给他设值, 但是能够从this.data中打印出来, 这是不是缓存的原因,还是其他的, 这个搞不太懂
你自己去了解js引用类型吧,而且我上面已经回复你为什么会这样了
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
请问你是设置checked的值时候和取值的时候的函数的顺序是什么,js的引用类型出现你第一张图的情况没什么奇怪的,能把代码全吗?
// 代码我给整理一下, 大致的逻辑是这样的
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) //得到实时数据
}
})
}
});
// 代码的逻辑又重新整理一下
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) //得到实时数据
}
})
}
});
你先输出checked的值,然后再去下次请求设置checked的值不是很正常吗?你设置checked值之后回调函数输出值就有值了(前提你返回的checked有值)
this.setData({ checked: checked }, () => {
console.log(this.data.checked);
});
现在的问题是这个checked还没有通过setData给他设值, 但是能够从this.data中打印出来, 这是不是缓存的原因,还是其他的, 这个搞不太懂
你自己去了解js引用类型吧,而且我上面已经回复你为什么会这样了