微信小程序开发问题请教
1.项目介绍
后面蒙层的工装列表是获取后台数据库('gongzhuang')渲染的,
领用清单里的数据则是存在本地缓存中,当点击button(确认清单)时,将领用清单里的数据通过for循环与.add添加至数据库('jilubiao')记录表里,并且通过for循环与.doc和.update修改数据库('gongzhuang')中的对应物品的库存字段‘kucun’。
问题:
但多台手机同时操作时,后台数据库('gongzhuang')的库存数量发生了变化,而手机端的本地缓存没有变化,继续用for循环与.doc和.update修改数据库('gongzhuang')则会导致库存数据错误。
我的解决方案:
通过for循环与.where在点提交按钮的瞬间校验本地缓存与数据库的库存数据是否一致,不一致则清本地缓存及提示刷新,但是新的问题是:不知道如何处理.where后返回来的数据。
//确认清单
submit(){
let cartList = wx.getStorageSync('cart')
let zhxinxi = wx.getStorageSync('zhanghuxinxi')
if (cartList.length<=0){
wx.showToast({
icon:'none',
title: '请添加物品',
})
}else if (!zhxinxi){
wx.showToast({
icon:'none',
title: '请去登入页登入',
})
setTimeout(()=>{
wx.navigateTo({
url: '/pages/login/login',
})
},1000);
return
}
// db.collection('gongzhuang').get()
// .then(res => {
// console.log('获取成功', res)
// this.setData({
// list: res.data
// })
// })
for(var i = 0; i<cartList.length; i++){
var shengyukucun = cartList[i].kucun - cartList[i].num
//提交前校验缓存与数据库的库存数据是否一致
// db.collection('gongzhuang')
// .where({
// _id: cartList[i]._id,
// kucun: cartList[i].kucun
// })
// .get()
// .then(res => {
// console.log('校验数据',res.data.length)
// this.setData({
// gongList:res.data.length
// })
// if(res.data.length<=0){
// console.log('不过')
// wx.showToast({
// icon:'none',
// title: '手速慢了,请重新刷新',
// })
// //清空领用清单
// wx.setStorageSync('cart', [])
// this.closeMask()
// }
// })
//修改多条数据
db.collection('gongzhuang')
.doc(cartList[i]._id)
.update({
data:{
kucun: shengyukucun
}
})
.then(res =>{
console.log('修改成功',res)
})
.catch(res =>{
console.log('修改失败',res)
})
//添加多条数据
db.collection('jilubiao')
.add({
data:{
name: cartList[i].name,
shuxing: cartList[i].shuxing,
shuliang: cartList[i].num,
lingyongren: zhxinxi[0].name,
banzu: zhxinxi[0].banzu,
lingyongtitle: new Date().getTime()
}
})
.then(res =>{
console.log('添加成功',res)
//清空领用清单
wx.setStorageSync('cart', [])
wx.switchTab({
url: '/pages/dingdan/dingdan',
})
})
.catch(res =>{
console.log('添加失败',res)
})
}
},
不一致,你就需要吧。where 渲染到 清单上 。获取到后 把值赋值给 清单列表的变量