收藏
回答

云数据库调用数据出错?

第一次调用不能获得数据,需要等待500ms再次调用才能获得数据(等待300ms都不可以)
代码如下:

  onLoad: function (options{
    this.today()
    setTimeout(()=>{
      this.today()
    }, 500)//这里很奇怪,不先调用数据库,然后延时一会继续调用数据库,数据会出错
  },

  today(){
    this.setData({
      taptype:'今日'
    })
    this.loadDataFromServer('today')
  },

  loadDataFromServer(type){//获得订单数量
    const now = new Date()
    const db = wx.cloud.database({
      env: 'yzktest-4g2sek3udaa6d365'
    }) 
    let clacDate;
    if(type === 'today'){ //计算当天收入
      clacDate = {
        Day:now.getDate(),
        Month:now.getMonth()+1,
        Year:now.getFullYear()
      }
    }else if(type === 'tomonth'){  //计算当月收入
      clacDate = {
        Month:now.getMonth()+1,
        Year:now.getFullYear()
      }
    }

    //获得订单信息并计算收益
    db.collection('Tourism_UserData').aggregate().match({
      payDate:clacDate,
      ifPay:1,
      ifrefund:1,
      area:this.data.pickedArea,
      acenic:this.data.pickedAcenic,
    }).count('datanum').end().then(res1 =>{
      if(res1.list.length === 0){
        this.setData({
          OrderNumber:0,
          WholeIncome:0,
          WholePromoteFee:Promotefees,
        })
      }else{
        const PAGE_COUNT = 20;
        const totalCount = res1.list[0].datanum;  //总数据数量
        const PageNum = Math.ceil(totalCount/PAGE_COUNT) //分页数
        let Pageindex = 0;
        let WholeIncome=0;
        for(Pageindex=0;Pageindex<PageNum;Pageindex++){  //每一页挨个处理
          db.collection('Tourism_UserData').aggregate().match({
            payDate:clacDate,
            ifPay:1,
            ifrefund:1,
            area:this.data.pickedArea,
            acenic:this.data.pickedAcenic,
          }).project({
            total_fee:1,
            refund_fee:1,
            promotorNum:1,
            promote_fee:1,
          }).skip(Pageindex*PAGE_COUNT).limit(PAGE_COUNT).end().then(res2 =>{
            const orderNum = res2.list.length;
            let i=0;
            for(i=0;i<orderNum;i++){
              WholeIncome = WholeIncome+res2.list[i].total_fee-res2.list[i].refund_fee;
            }
            //console.log(Promotefees)
            this.setData({
              WholeIncome:WholeIncome,
            })
          })
        }
        this.setData({
          OrderNumber:totalCount
        })
      }
    })
  },



回答关注问题邀请回答
收藏
登录 后发表内容