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日

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元