1.数据库
1.1请求
Page ({onLoad() {// 固定写法wx.cloud.database().collection('goods').get({ // 查询操作// 请求成功success(res) {console.log("请求成功",res)},// 请求失败fail(err) {console.log("请求失败",err)}})// es6的简介写法wx.cloud.database().collection('goods').get().then(res=>{ // 请求成功console.log("第二种方法请求成功",res)}).catch(err=>{ // 请求失败console.log("第二种方法请求失败",err)})}})
1.2将数据写入页面
Page ({data:{list:[]},=====// es6的简介写法wx.cloud.database().collection('goods').get().then(res=>{ // 请求成功console.log("第二种方法请求成功",res)this.setData({list:res.data})})<view wx:for="{{list}}"><view>商品名:{{item.name}},价格:{{item.price}}</view></view>
// 固定写法// let that = this// console.log("onload",this)// wx.cloud.database().collection('goods')// .get({ // 查询操作// // 请求成功// success(res) {// // console.log("请求成功", res)// // console.log(this)// that.setData({// list: res.data// })// },// // 请求失败// fail(err) {// console.log("请求失败", err)// }// })
onLoad() {// es6的简介写法wx.cloud.database().collection('goods').get().then(res => { // 请求成功 箭头函数的this是他父亲的console.log("第二种方法请求成功", res)console.log("第二种", this)this.setData({list: res.data})}).catch(err => { // 请求失败console.log("第二种方法请求失败", err)})}
1.3查询(where doc)
where
onLoad() {// es6的简介写法wx.cloud.database().collection('goods').where({ // 条件查询name:'苹果'}).get().then(res => { // 请求成功 箭头函数的this是他父亲的this.setData({list: res.data})}).catch(err => { // 请求失败console.log("第二种方法请求失败", err)})}
1.4添加(add)
add页面
Page({onLoad() {wx.cloud.database().collection("goods").add({ // 添加数据data:{name:'车厘子',price: 200}}).then(res => {console.log("添加成功",res)}).catch(err => {console.log("添加失败", err)})}})
1.5更新(update)
update() {wx.cloud.database().collection('goods').doc('fa24ce1a618ba2b2056f4d3e2038a03a').update({ // 修改data:{price:100}}).then(res => {console.log("修改成功", res)}).catch(err => {console.err("修改失败", err)})}
1.6删除(remove)
remove() {wx.cloud.database().collection('goods').doc('9e7190f1618ba67804ce4da01e1d8f6c') // 要删除数据的 _id.remove().then(res => {console.log("删除成功", res)}).catch(err => {console.err("删除失败", err)})}
