李权2020929

云服务:

// pages/index1/index1.js
const db = wx.cloud.database({
  env:'lq123456-2gduf9cac80c09f0',
});//初始化数据库

Page({

  /**
   * 页面的初始数据
   */
  data: {
      fileID:'',
  },
  //增加数据
  add(){
    db.collection('student').add({
      data:{
        name:'张林',
        age:21,
        height:170
      }
    }).then(res=>{
      console.log(res);
      
    }).catch(err=>{
      console.log(err);
      
    })
  },
  //查询数据
  select(){
    //获取所有数据的写法
    // db.collection('student').get().then(res=>{
    //   console.log(res); 
    // }).catch(err=>{
    //   console.log(err);
    // })

    //通过id获取数据 id,微信小程序有一个doc()
    // db.collection('student').doc('e373396c5f73317c00b40a801a1eb91d')
    // .get().then(res=>{
    //   console.log(res);
    // }).then(err=>{
    //   console.log(err);
    // })

    //通过wehere查询多个语句
    // db.collection('student').where({
    //   age:18
    // }).get().then(res=>{
    //   console.log(res);
      
    // }).catch(err=>{
    //   console.log(err);
      
    // })
    //查询指令
  //   const _ = db.command;
  //   db.collection('student').where({
  //     age:_.gt(18).and(_.lt(22))
  //   }).get().then(res=>{
  //     console.log(res);
  //   }).catch(err=>{
  //     console.log(err);
  //   })
  // },
    //查询多个条件
    const _ = db.command;
    db.collection('student').where(_.or([
      {
        age:_.gte(19)
      },
      {
        height:_.gt(170)
      }
    ])).get().then(res=>{
 
    }).catch(err=>{
      console.log(err);
    })
  },
  updata(){
    db.collection('student').doc('c54bd3a25f7332870094f6463ac78305').update({
      data:{
        name:'冯强',
      }
    }).then(res=>{
      console.log(res);
      
    }).catch(err=>{
      console.log(err);
      
    })
  },
  //上传文件
  addFile(){
    wx.chooseImage({
      count: 3,
      sourceType:['album','camera'],
      sizeType:['compressed','original'],
    }).then(res=>{
      for(let i=0;i<res.tempFilePaths.length;i++){
        wx.cloud.uploadFile({
          cloudPath:new Date().getTime()+".jpg",
          filePath:res.tempFilePaths[i]
        }).then(res=>{
          db.collection('img').add({
            data:{
              fileID:res.fileID,
            }
          })
        }).catch(err=>{
          console.log(err);
        })
      }
    }).catch(res=>{
      console.log(res);
    })
  },
  show(){
    let This = this;
    db.collection('img').doc('8e5be7055f73418700d4708301117657').get().then(res=>{
      This.setData({
        fileID:res.data.fileID,
      })
    }).then(err=>{
      console.log(err);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

学习心得:

今天学习云服务有点伤,明天搞云函数

评论