一、分清几个概念
1、云开发,简言之就是可以直接用微信小程序开发者工具完成一个从前台到后台的小程序项目。
2、小程序端,使用云开发的时候,miniprogram中写的代码可以叫做小程序端(真实是我不知道可以不可以这么认为)。(在创建小程序项目勾选小程序云开发会自动生成miniprogram文件和cloudfunctions文件)
3、云函数,cloudfunctions中写的就是云函数了。(在创建小程序项目勾选小程序云开发会自动生成miniprogram文件和cloudfunctions文件)
4、云数据库和云存储,可以看做数据库了只是换了一个名字。云数据库里面存放集合(官方名称),集合里面存放记录(官方名称);云存储中可以存放图片等
二、云数据库中手动创建的集合,权限设置为’所有用户可读,仅创建者可读写’
三、小程序端连接云数据库实现增删改查(基础的),然后小程序端可以直接调用云数据库中的数据
1.小程序端一次增加一条或者多条记录
wx.cloud.database().collection('test').add({data: {字段名1: 值1,字段名2: 值2 ... ...}}).then((res) => {console.log(res)//返回的res里面有_id的值,这个_id是系统自动生成的。}).catch(err=>{console.log(err)})/***若想已经存在的记录增加字段,就要用到update了。*/
2.小程序端查找一条或者多条或是全部记录
//一、获取某一条记录
wx.cloud.database().collection('test').doc('_id的值').get()
.then(res => {console.log(res)}).catch(err => {console.log(err)})
/*_id的值可以从云数据库中的集合中的记录中,复制自己想要的字段里面的_id的值。*///二、获取某个集合中的所有记录,但是小程序端中默认只能获取20条数据且最多是20条wx.cloud.database().collection('test').get().then(res => {console.log(res)//返回的res里面有_id的值,这个_id是系统自动生成的。}).catch(err => {console.log(err)})// 三、where查询,指定查询条件
//1.使用数据库集合查询wx.cloud.database().collection('test').where({字段名1:值1}).get().then(res=>{console.log(res)})//返回的res里面有_id的值,这个_id是系统自动生成的。
/*值的类型需要与云数据库表中的记录中的相字段名的值一致,如都是Number类型。查询条件可以有多个,
用逗号隔开。*///2.使用数据库操作符查找
const db = wx.cloud.database()
const _ = db.command
db.collection('test').where({字段名: _.eq(value)}).get().then(res=>{console.log(res)})
/*筛选出集合中与value值相等的记录,数据类型也要一致*/// 四、指定返回结果中记录需返回的字段
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'的数组(自测得)。**/
3.小程序端删除一条记录
wx.cloud.database().collection('test').doc('_id的值')
.remove().then((res) => {console.log(res.stats)}).catch(err=>{console.log(err)})
/*_id的值可以从云数据库中的集合中的记录中复制自己想要的字段里面的_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的值可以从云数据库中的集合中的记录中复制自己想要的字段里面的_id的值。*///2.set,会删除原有的的字段,但不会删除系统自动生成的字段,
/*如果不想原有的某些字段被删就要set数据的时候带上*/
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的值可以从云数据库中的集合中的记录中复制自己想要的字段里面的_id的值。*/
5.分页显示后,过滤数据
data: {list:[],num:2},
//1.onLoad中从云数据库中获取集合中的所有数据/*** 生命周期函数--监听页面加载*/
onLoad: function (options) {// 小程序端默认只能获取20条数据且最多是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.页面上拉加载时,
/*** 页面上拉触底事件的处理函数*/
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.过滤数据
/*test.wxml中判断获取的数组中是否有某字段,如,wx:if="{{item.isAlreadyPay?ture:false}}",
如果该字段存在在数据就显示否则就不显示,前提是已经用isAlreadyPay字段记下了用户的操作了。*/