+数量增加方法,我点击按钮,最起码要等一秒数据才在页面上完成更新渲染,控制台是会先打印出‘已更新已渲染’但数据往往是要子啊过一会儿才会增加
add: function (e) {
db.collection('secShopcart').doc(e.target.dataset.id).update({
data:{
count:_.inc(1)
}
}).then(res => {
console.log("已更新")
var that = this;
db.collection('secShopcart').get().then(res => {
that.setData({
goods: res.data,
// count:res.data
})
this.setCart(res.data) //统计件数与价格
})
console.log("已渲染")
})
},
<view wx:for="{{goods}}" wx:key="_id">
<van-card title='{{item.title}}' desc='{{item.seller}}' price="{{item.price}}" thumb='{{item.image}}'
num='{{item.count}}'>
<!-- <view slot="tags">
<view wx:for="{{item.tags}}" wx:key="_id" wx:for-index="idx" wx:for-item="tag">
<view wx:if="{{idx < 2}}">
<van-tag type="primary">{{tag.name}}</van-tag>
</view>
</view>
</view> -->
<view slot="footer">
<van-button size="mini" data-id="{{item._id}}" type="default" plain bind:click="del">-</van-button>
<van-button size="mini" data-id="{{item._id}}" type="default" plain bind:click="add">+</van-button>
<van-checkbox class="check" style="display:{{shows}}" name="{{ item._id }}" bind:change="onChange"
data-id="{{item._id}}">
</van-checkbox>
</view>
</van-card>
</view>
add: function (e) {
db.collection('secShopcart').doc(e.target.dataset.id).update({
data: {
count: _.inc(1)
}
})
let newgoods = this.data.goods
let good = newgoods.find(v => v._id == e.target.dataset.id)
good.count += 1
this.setData({
goods: newgoods
})
this.setCart(newgoods)
},
为什么要等数据库更新呢?