上拉加载更多后台数据_微信小程序端操作云数据库
一、分清幾個概念
1、云開發(fā),簡言之就是可以直接用微信小程序開發(fā)者工具完成一個從前臺到后臺的小程序項目。
2、小程序端,使用云開發(fā)的時候,miniprogram中寫的代碼可以叫做小程序端(真實是我不知道可以不可以這么認為)。(在創(chuàng)建小程序項目勾選小程序云開發(fā)會自動生成miniprogram文件和cloudfunctions文件)
3、云函數(shù),cloudfunctions中寫的就是云函數(shù)了。(在創(chuàng)建小程序項目勾選小程序云開發(fā)會自動生成miniprogram文件和cloudfunctions文件)
4、云數(shù)據(jù)庫和云存儲,可以看做數(shù)據(jù)庫了只是換了一個名字。云數(shù)據(jù)庫里面存放集合(官方名稱),集合里面存放記錄(官方名稱);云存儲中可以存放圖片等
二、云數(shù)據(jù)庫中手動創(chuàng)建的集合,權限設置為’所有用戶可讀,僅創(chuàng)建者可讀寫’
三、小程序端連接云數(shù)據(jù)庫實現(xiàn)增刪改查(基礎的),然后小程序端可以直接調(diào)用云數(shù)據(jù)庫中的數(shù)據(jù)
1.小程序端一次增加一條或者多條記錄
wx.cloud.database().collection('test').add({data: {字段名1: 值1,字段名2: 值2 ... ...}}).then((res) => {console.log(res)//返回的res里面有_id的值,這個_id是系統(tǒng)自動生成的。}).catch(err=>{console.log(err)})/***若想已經(jīng)存在的記錄增加字段,就要用到update了。*/2.小程序端查找一條或者多條或是全部記錄
//一、獲取某一條記錄 wx.cloud.database().collection('test').doc('_id的值').get() .then(res => {console.log(res)}).catch(err => {console.log(err)}) /*_id的值可以從云數(shù)據(jù)庫中的集合中的記錄中,復制自己想要的字段里面的_id的值。*///二、獲取某個集合中的所有記錄,但是小程序端中默認只能獲取20條數(shù)據(jù)且最多是20條wx.cloud.database().collection('test').get().then(res => {console.log(res)//返回的res里面有_id的值,這個_id是系統(tǒng)自動生成的。}).catch(err => {console.log(err)})// 三、where查詢,指定查詢條件 //1.使用數(shù)據(jù)庫集合查詢wx.cloud.database().collection('test').where({字段名1:值1}).get().then(res=>{console.log(res)})//返回的res里面有_id的值,這個_id是系統(tǒng)自動生成的。 /*值的類型需要與云數(shù)據(jù)庫表中的記錄中的相字段名的值一致,如都是Number類型。查詢條件可以有多個, 用逗號隔開。*///2.使用數(shù)據(jù)庫操作符查找 const db = wx.cloud.database() const _ = db.command db.collection('test').where({字段名: _.eq(value)}).get().then(res=>{console.log(res)}) /*篩選出集合中與value值相等的記錄,數(shù)據(jù)類型也要一致*/// 四、指定返回結(jié)果中記錄需返回的字段 wx.cloud.database().collection('test').field( {字段名1: true,字段名2:true,字段名3:false... ...}).get().then(res => {console.log(res.data)}).catch(err => {console.log(err)}) /**對象的各個字段名表示要返回或不要返回的字段,value 傳入 true|false(或 1|-1)表示要 返回還是不要返回。但是沒有該字段名但是值為true,不會報錯只會輸出'_id'的數(shù)組(自測得)。**/3.小程序端刪除一條記錄
wx.cloud.database().collection('test').doc('_id的值') .remove().then((res) => {console.log(res.stats)}).catch(err=>{console.log(err)}) /*_id的值可以從云數(shù)據(jù)庫中的集合中的記錄中復制自己想要的字段里面的_id的值。*/4.更新一條或者多條記錄
//1.update,不會刪除原有的添加新字段 wx.cloud.database().collection('test').doc('_id的值').update({data:{字段名1: 值1,字段名2:值2,字段名3:值3... ...}}).then((res) => {console.log(res.stats)}).catch(err=>{console.log(err)}) /*_id的值可以從云數(shù)據(jù)庫中的集合中的記錄中復制自己想要的字段里面的_id的值。*///2.set,會刪除原有的的字段,但不會刪除系統(tǒng)自動生成的字段, /*如果不想原有的某些字段被刪就要set數(shù)據(jù)的時候帶上*/ wx.cloud.database().collection('test').doc('_id的值').set({data:{字段名1: 值1,字段名2:值2,字段名3:值3... ...}}).then((res) => {console.log(res.stats)}).catch(err=>{console.log(err)}) /*_id的值可以從云數(shù)據(jù)庫中的集合中的記錄中復制自己想要的字段里面的_id的值。*/5.分頁顯示后,過濾數(shù)據(jù)
data: {list:[],num:2}, //1.onLoad中從云數(shù)據(jù)庫中獲取集合中的所有數(shù)據(jù)/*** 生命周期函數(shù)--監(jiān)聽頁面加載*/ onLoad: function (options) {// 小程序端默認只能獲取20條數(shù)據(jù)且最多是20條wx.cloud.database().collection('test').skip(0).limit(20).get().then(res => {console.log('獲取成功', res)this.setData({list: res.data})}).catch(res => {console.log('獲取失敗', res)}) }, //2.頁面上拉加載時, /*** 頁面上拉觸底事件的處理函數(shù)*/ onReachBottom: function () {console.log('加載更多。。。')wx.cloud.database().collection('test').skip((this.data.num - 1) * 20).limit(20).get().then(res => {console.log('獲取成功', res)this.setData({// 拼接list: this.data.list.concat(res.data),num: this.data.num + 1})}).catch(res => {console.log('獲取失敗', res)}) }, //3.過濾數(shù)據(jù) /*test.wxml中判斷獲取的數(shù)組中是否有某字段,如,wx:if="{{item.isAlreadyPay?ture:false}}", 如果該字段存在在數(shù)據(jù)就顯示否則就不顯示,前提是已經(jīng)用isAlreadyPay字段記下了用戶的操作了。*/總結(jié)
以上是生活随笔為你收集整理的上拉加载更多后台数据_微信小程序端操作云数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cisco设备做流量监控的方法
- 下一篇: 数据库的事务级别介绍与操作