uniapp小程序云开发文档
1.初始化
App.vue
onLaunch:function(){
wx.cloud.init({
env:''//开通好的云开发环境变量
})
}
2数据库超作
查get()
wx.cloud.database().collection('集合名称').get({
success(res){
console.log('请求成功')
},
fail(err){
console.log('请求失败')
}
})
ES6语法
wx.cloud.database().collection('集合名称').get().then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
单条数据查询doc()
wx.cloud.database().collection('集合名称').doc('id').then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
条件查询where()
wx.cloud.database().collection('集合名称').where({
//对象写法 gt是大于,gte是大于等于,lt是小于,lte是小于等于
num:wx.cloud.database().command.gt(100)
}).then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
//多条件查询
let db = wx.cloud.database()
let _=db.command
.where(_.and([
{num:_gt(10)},
{num:lt(20)}
]))
新增数据add()
wx.cloud.database().collection('集合名称').add({
data:{
//对应的字段
}
}).then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
更新数据update()
wx.cloud.database().collection('集合名称').doc('id').update({
data:{
//对应的修改字段
}
}).then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
删除数据remove()
wx.cloud.database().collection('集合名称')
.doc('id')
.remove()
.then(res=>{
console.log('请求成功')
}).catch(err=>{
console.log('请求失败')
})
数据排序orderBy()
wx.cloud.database().collection('集合名称')
.orderBy('根据字段','asc/desc')
.get()
返回指定条数limit()
wx.cloud.database().collection('集合名称')
.lime('指定条数')
.skip('指定从第几条数据')
.get()
3开启云函数uniapp
manifest.json //文件中找到
mp-weixin:{
//加入代码开启云服务
"cloudfunctionRoot":"./cloud/"
}