收藏
回答

为什么这里还没有获取到数据前端就渲染完毕?

goodlist是在数据库Good表里查postlist里的每个帖子是否被当前用户点赞,pid:帖子id,uid:点赞用户的id

然后前端循环postlist,点赞状态用goodlist里的数据来判断显示,这里console一下goodlist里的数据是有的,但前端没有判断成功是为什么?

onShow: function (e{
        this.getpos();
    },
getpos(){
        var  c= wx.getStorageSync('com');
        var id = wx.getStorageSync('info').id;
        var that = this;
        var list=[];
            db.collection('Post').where({  //获取全部贴文
                major:c,
            }).orderBy('time','desc').get()
            .then(res=>{
                that.setData({
                    postlist:res.data
                })
                for(let i =0;i<res.data.length;i++)
                {
                    db.collection('Good')
                    .where({
                        pid:res.data[i]._id,
                        uid:id,
                    })
                    .get()
                    .then(res=>{
                        if(res.data.length==0)   //没有当前用户的点赞信息
                        {
                            list.push(false)
                        }
                        else
                            list.push(true)
                    })
                    if(i+1==res.data.length)
                    {
                        console.log(list)
                        that.setData({
                            goodlist:list
                        })
                    }
                }
            })
            .catch(err=>{
                console.log("无法获取",err)
            })
        }
    },

<view wx:for="{{postlist}}" wx:for-item="item" style="background-color: white;" >
    <image wx:if="{{!goodlist[index]}}" src="../../image/good.png" class="icon" style="right: 100rpx;position: absolute;" bindtap="good" data-index="{{index}}"></image>
    <image wx:elif="{{goodlist[index]}}" src="../../image/good_2.png" class="icon" style="right: 100rpx;position: absolute;" bindtap="ungood" data-index="{{index}}"></image>
</view>


页面都显示good.png

回答关注问题邀请回答
收藏
登录 后发表内容