小程序云开发记录

最近自己在做个人微信小程序,用的云开发,其中小程序原生很多语法和惯用的不一样,记录其中踩的坑吧

(1)数据库数据结构如下

    {
        _openid: "1",
        myData: [{
            startDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            endDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            time: 3
        }, {
            startDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            endDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            time: -1
        }]
    }

  我想要在云函数中更新myData索引为2的time值,update方法无法实现

 换种思路,将数组变成对象,key值为索引值

{
        _openid: "1",
        myData: {
        "0":{
            startDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            endDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            time: 3
        }, 
       "1": {
            startDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            endDate: "Tue Oct 29 2019 00:00:00 GMT+0800 (中国标准时间)",
            time: -1
        }
        }
}            

 云函数中修改对应索引的time值安排(更新time值):

  //获取要修改的对象索引值 以及该对象原来的参数time值
    let curData = {
    index:1,
    time:-1 //当前索引值原来的时间
    }
 let upStr = curData.index; 
        monthList.doc(_id).update({
          data: {
           myData: {
              [upStr]: {
                time: Number(curData.time) + Number(event.time)//event.time为当前传递的参数
              }
            }
          }
        })

  

………………后续在增加记录 

猜你喜欢

转载自www.cnblogs.com/anu0823/p/12463602.html