VenusYan 2023-04-20 15:20 采纳率: 100%
浏览 47
已结题

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

小程序云开发数据库异步问题
进行测试时发现写入的请假会被初始写入覆盖掉,自己加了一个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;i<n;i++) {
        try {
          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;i<n2;i++) {
        try {
          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()
})

  • 写回答

3条回答 默认 最新

  • 喝茶品人生 2023-04-20 15:40
    关注

    原因在于你的上下午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()
    })
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月28日
  • 已采纳回答 4月20日
  • 创建了问题 4月20日

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄