小程序云开发数据库异步问题

小程序云开发数据库异步问题
进行测试时发现写入的请假会被初始写入覆盖掉,自己加了一个promise all但是发现没有用,请问应该怎么解决

  //添加今日字段
  const checkin1 = db.collection("checkin1");
  let $ = db.command.aggregate
  checkin1.aggregate().addFields({ //添加新字段
    totalPrice:$.multiply([y+m+d+'A'])
  })
  let promises = [];
  //初始写入上午
  promises.push(db.collection("classtime1").where({_id:`${w}`+'A'}).get().then( res => {
    if (res.data[0].startime == null || res.data[0].startime === ''){
      try {
        checkin1.where({
        }).update({
           data: {
            [[y+m+d+'A']] : '#ED1C24'
          },
        })
      } catch(e) {
        console.error(e)
      }
    }else{
      try {
        checkin1.where({
        }).update({
           data: {
            [[y+m+d+'A']] : '#C3C3C3'
          },
        })
      } catch(e) {
        console.error(e)
      }
    }
  }))
  //初次写入下午
  promises.push(db.collection("classtime1").where({_id:`${w}`+'P'}).get().then( res => {
    if (res.data[0].startime == null || res.data[0].startime === ''){
      try {
        checkin1.where({
        }).update({
           data: {
            [[y+m+d+'P']] : '#ED1C24'
          },
        })
      } catch(e) {
        console.error(e)
      }
    }else{
      try {
        checkin1.where({
        }).update({
           data: {
            [[y+m+d+'P']] : '#C3C3C3'
          },
        })
      } catch(e) {
        console.error(e)
      }
    }
  }))
  Promise.all(promises).then(() => {
  //写入上午请假
  db.collection("leave").where({_id:y+m+d+'A'}).get().then( res => {
    if (res.data.length != 0){
      tphone = res.data[0].phone;
      const n=tphone.length;
      for (i=0;itry {
          checkin1.where({_id:parseInt(tphone[i])
          }).update({
            data: {
              [[y+m+d+'A']] : '#99D9EA'
            },
          })
        } catch(e) {
          console.error(e)
        }
      }
      console.log(y+m+d+'A');
    }
  })
  //db.collection("leave").where({_id:y+m+d+'A'}).remove()
  //写入下午请假
  db.collection("leave").where({_id:y+m+d+'P'}).get().then( res => {
    if (res.data.length != 0){
      tphone2 = res.data[0].phone;
      const n2=tphone2.length;
      for (i=0;itry {
          checkin1.where({_id:parseInt(tphone2[i])
          }).update({
            data: {
              [[y+m+d+'P']] : '#99D9EA'
            },
          })
        } catch(e) {
          console.error(e)
        }
      }
    }
  })
  //db.collection("leave").where({_id:y+m+d+'P'}).remove()
})

原因在于你的上下午promise异步了,修改成这样:

Promise.all(promises).then(() => {
  db.collection("leave").where({_id:y+m+d+'A'}).get().then( res => {
    if (res.data.length != 0){
      tphone = res.data[0].phone;
      const n=tphone.length;
      let promises2 = []; 
      for (i=0;i<n;i++) {
        promises2.push(checkin1.where({_id:parseInt(tphone[i])
          }).update({
            data: {
              [[y+m+d+'A']] : '#99D9EA'
            },
          }))
      }
      Promise.all(promises2).then(() => {
        console.log(y+m+d+'A');
      }).catch((e) => {
        console.error(e);
      })
    }
  })
  //db.collection("leave").where({_id:y+m+d+'A'}).remove()
  db.collection("leave").where({_id:y+m+d+'P'}).get().then( res => {
    if (res.data.length != 0){
      tphone2 = res.data[0].phone;
      const n2=tphone2.length;
      let promises3 = []; 
      for (i=0;i<n2;i++) {
        promises3.push(checkin1.where({_id:parseInt(tphone2[i])
          }).update({
            data: {
              [[y+m+d+'P']] : '#99D9EA'
            },
          }))
      }
      Promise.all(promises3).then(() => {
        console.log(y+m+d+'P');
      }).catch((e) => {
        console.error(e);
      })
    }
  })
  //db.collection("leave").where({_id:y+m+d+'P'}).remove()
})


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632