小程序云开发(上)

小程序云开发(上),第1张

小程序云开发官方文档

数据库的增删改查
// app.js
App({
  // 小程序已启动就会执行
  onLaunch() {
    wx.cloud.init({
      env: 'yunkaifa-9gjrckmo794e04e8' //云开发环境Id
    })
  }
})

创建一个商品数据表并添加数据


使用get方法请求数据库里的数据 修改数据表的读写权限 shujuku.js
Page({
    onLoad() {
        // 传统的固定写法
        wx.cloud.database().collection('goods')
            // 查询 *** 作
          .get({
            // 请求成功
            success(res) {
                console.log('请求成功', res)
            },
            // 请求失败
            fail(err) {
                console.log('请求失败', err)
            }     
          })  
    }
})
使用get方法请求数据库里的数据(es6简洁写法) shujuku.js
Page({
    onLoad() {
        // es6的简洁写法
        wx.cloud.database().collection('goods').get()
        .then(res => {  // 请求成功
            console.log('《请求成功', res)
        })
        .catch(err => {   // 请求失败
            console.log('《请求失败', err)
        })
    }
})
把请求到的数据显示到小程序页面 shujuku.wxml
<view wx:for="{{list}}">
    <view>商品名: {{item.name}}, 价格: {{item.price}}view>
view>
shujuku.js
Page({
    data: {
        list: []
    },
    onLoad() {
        // es6的简洁写法
        wx.cloud.database().collection('goods').get()
        .then(res => {  // 请求成功
            console.log('《请求成功', res.data)
            // 动态请求
            this.setData({
                list: res.data
            })
        })
        .catch(err => {   // 请求失败
            console.log('《请求失败', err)
        })
    }
})
shujuku.js
Page({
    data: {
        list: []
    },
    onLoad() {
        console.log('onload里的this', this)
        let that = this
        // 传统的固定写法
        wx.cloud.database().collection('goods')
            // 查询 *** 作
          .get({
            // 请求成功
            success(res) {
                console.log('请求成功', res)
                that.setData({
                    list: res.data
                })
            },
            // 请求失败
            fail(err) {
                console.log('请求失败', err)
            }     
          })
    }
})
使用where查询符合条件的数据
Page({
    data: {
        list: []
    },
    onLoad() {     
        // es6的简洁写法
        wx.cloud.database().collection('goods')
        .where({
            name: '苹果' 
        })
        .get()
        .then(res => { 
            this.setData({
                list: res.data
            })
        })
        .catch(err => {   // 请求失败
            console.log('《请求失败', err)
        })
    }
})
查询单条数据doc()

doc是用来查询单条数据的,比如,商品详情页

shujuku.js
Page({
    data: {
        list: [],
        good: {}
    },
    onLoad() {     
        wx.cloud.database().collection('goods').get()
        .then(res => {  
            console.log('请求成功', res.data)
            this.setData({
                list: res.data
            })
        })
        .catch(err => {   
            console.log('请求失败', err)
        })
        // 使用doc查询单条数据
        wx.cloud.database().collection('goods')
            .doc('2c9907ee625a2cf3002edf1c44cc9425')
            .get()
            .then(res => { 
                console.log('查询单条数据成功', res.data)
                this.setData({
                    good: res.data
                })
            })
            .catch(err => {  
                console.log('查询单条数据失败', err)
            })
    }
})
shujuku.wxml
<view wx:for="{{list}}">
    <view>商品名: {{item.name}}, 价格: {{item.price}}view>
view>
<view>
doc查询的单条数据:{{good.name}},价格:{{good.price}}
view>

通过add方法添加新数据 add.js
Page({
    onLoad() {
        
    },
    // 添加数据
    add() {
        wx.cloud.database().collection('goods')
        // 添加数据
        .add({
            data: {
                name: '车厘子',
                price: '200'
            }
        })
        .then(res=>{
            console.log('添加成功', res)
        })
        .catch(err=>{
            console.log('添加失败', err)
        })
    }
})
add.wxml

<button bindtap="add">点击添加按钮button>
更新数据update()

修改数据库里已存在的数据

add.wxml

<button bindtap="add">点击添加按钮button>
<button bindtap="update">修改数据button>
add.js
Page({
    onLoad() {
        
    },
    // 添加数据
    add() {
        wx.cloud.database().collection('goods')
            // 添加数据
            .add({
                data: {
                    name: '车厘子',
                    price: '200'
                }
            })
            .then(res=>{
                console.log('添加成功', res)
            })
            .catch(err=>{
                console.log('添加失败', err)
            })
    },
    update() {
        wx.cloud.database().collection('goods')
            .doc('1fee1e97625a32320032242577a0aea6')
            .update({
                data: {
                    price: '100'
                }
            })
            .then(res=>{
                console.log('修改成功', res)
            })
            .catch(err=>{
                console.log('修改失败', err)
            })
    }
})

删除数据remove() add.wxml

<button bindtap="add">点击添加按钮button>
<button bindtap="update">修改数据button>
<button bindtap="remove">删除单条数据button>
add.js
Page({
    onLoad() {
        
    },
    // 添加数据
    add() {
        wx.cloud.database().collection('goods')
            // 添加数据
            .add({
                data: {
                    name: '车厘子',
                    price: '200'
                }
            })
            .then(res=>{
                console.log('添加成功', res)
            })
            .catch(err=>{
                console.log('添加失败', err)
            })
    },
    update() {
        wx.cloud.database().collection('goods')
            .doc('1fee1e97625a32320032242577a0aea6')
            .update({
                data: {
                    price: '100'
                }
            })
            .then(res=>{
                console.log('修改成功', res)
            })
            .catch(err=>{
                console.log('修改失败', err)
            })
    },
    remove() {
        wx.cloud.database().collection('goods')
            .doc('1fee1e97625a32320032242577a0aea6')
            .remove()
            .then(res=>{
                console.log('删除成功', res)
            })
            .catch(err=>{
                console.log('删除失败', err)
            })
    }
})
增删改查综合案例
  1. 查看商品列表
  2. 动态添加商品
  3. 进入商品详情页
  4. 删除某个商品
  5. 修改某个商品价格
修改页面标题

demo1.json
{
  "usingComponents": {},
  "navigationBarTitleText": "商品列表页"
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/736314.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存