这里我想调用getgood函数将数据保存到goodlist中,再显示到页面中,但在加载完毕前已经渲染完了,不显示已经加载的数据,如何解决?
onLoad: function (options) {
this.getData();
this.getpos();
},
getpos(){
var c= wx.getStorageSync('com');
var that = this;
db.collection('Post').where({ //获取全部贴文
major:c,
}).orderBy('time','desc').get()
.then(res=>{
that.setData({
postlist:res.data
})
that.getgood().then(res=>{
console.log(res)
})
})
.catch(err=>{
console.log("无法获取",err)
})
},
getgood(){
var poslist = this.data.postlist;
var list = [];
var id = wx.getStorageSync('info').id
var that = this;
return new Promise((resolve, reject) => {
for(let i =0;i<poslist.length;i++)
{
db.collection('Good')
.where({
pid:poslist[i]._id,
uid:id,
})
.get()
.then(res=>{
if(res.data.length==0)
{
list.push(false)
}
else
list.push(true)
})
if(i+1==poslist.length)
{
that.setData({
goodlist:list
})
resolve(list);
}
}
})
},
模板中绑定的是goodlist吗
<view wx:if="{{goodlist[index]==false}}">
<image src="../../image/good.png" class="icon" style="right: 100rpx;position: absolute;" bindtap="good" data-index="{{index}}"></image>
</view>
<view wx:elif="{{goodlist[index]==true}}">
<image src="../../image/good_2.png" class="icon" style="right: 100rpx;position: absolute;" bindtap="ungood" data-index="{{index}}"></image>
</view>