小程序的云开发(一)

首先,小程序如果要应用到云服务,例如小程序自带的云数据库,在新建小程序时勾选云开发,不再使用测试号。
其次,点开云开发新建自己的云环境,界面如下
在这里插入图片描述
云环境的名称自己设置,id在app.js这个文件里用到

App({
  onLaunch: function () {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // env 参数说明:
        //   env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境(第一个创建的环境)
         env: '自己的id',
        traceUser: true,
      })
    }

    this.globalData = {}
  }
})

在云开发内,进入数据库,可以增改数据库和在数据库内增加字段记录
在这里插入图片描述
注意右侧的数据权限,要选择所有人可读,创建人可读写,下面介绍如果对数据库进行搜索,添加,删除,更新这四种操作。
首先,搜索,我们先用input组件输入一个关键字,再对其进行搜索,下面为简单的页面代码,后面不再更改

<input class="input" bindinput="input"></input>
<input class="input" bindinput="input2"></input>
<view class="container1">
<button type="primary" bindtap="button">搜索</button>
<button type="primary" bindtap="button2">添加</button>
<button type="primary" bindtap="button3">删除</button>
<button type="primary" bindtap="button4">更新</button>
<view>名字:{{name}}</view>
<view>密码:{{password}}</view>
<view>ID:{{id}}</view>
</view>

下面这段为搜索的代码,较长,可以用作查找后将记录显示,或者验证用户名密码,或者搜索到之后跳转

input(e){
  username = e.detail.value
  console.log("username"+username)
},
input2(e){
  password = e.detail.value
  console.log("password"+password)
},
button(){
   //判断搜索内容是否为空
   if(username&&username.length>0)
   { //搜素写法
    wx.cloud.database().collection("test").where({
      name: wx.cloud.database().RegExp({
        regexp:username,
        //i不区分大小写
        option:"i"
       
      })
    }).get()
    .then(res => {
      //下面切换不一样的方式去验证密码或者显示搜索词条
     /* console.log("搜索结果", res.data[0].password)
      if(res.data[0].password==password)
      {
        wx.showToast({
          icon:"none",
          title: 'bingo',
        })
      }*/
    console.log("搜索结果", res.data[0])
    console.log("搜索结果", res.data.length)
      if(res.data.length!=0)
     {
      this.setData({
      
        name:res.data[0].name,
        password:res.data[0].password,
        id:res.data[0]._id
      })
     /*  wx.navigateTo({
        url: '/pages/test/test?username=' + username,
      })*/
    }
      else
      wx.showToast({
        icon:"none",
        title: '空',
      })
    })
    .catch(res => {
      console.log("搜索失败", res)
    })
  }
    
    else
    {
      wx.showToast({
        icon:"none",
        title: '空',
      })
    }
},

增加记录功能:这里很简单的将增加的词条写在js里,实际上应该由input输入,获取再增加至数据库

button2(){
  
  wx.cloud.database().collection("test").add({
      data:{
        name:'jerry',
        password:123,
      
      }
    }) .then(res=>{
      console.log(res)
      wx.hideLoading({
        success: (res) => {},
      })
    })
  },

删除功能:如果用doc那么就id进行查找,id是形成记录后系统自行分配的,用where的话可以对指定字段进行删除,这里也将需要删除的字段写在了js里,实际应用也应该由input输入后删除,可以参考查找功能

button3(){
  //wx.cloud.database().collection("test").where({_id:'e737e89d633ced530064376f5a00ee54'}).remove().then(res=>{
    wx.cloud.database().collection("test").doc('e737e89d633ced530064376f5a00ee54').remove().then(res=>{
    console.log(res)
    wx.hideLoading({
      success: (res) => {},
    })
  })
},

更新功能:更新功能可以对已经存在的字段内容进行更改,也可以添加新的字段内容

button4(){
    wx.cloud.database().collection("test").where({_id:'971357ce633ea0ab00798f6910597c12'}).update({
      data:{
        //password:456,
        update:'1',
      }
    }).then(res=>{
    console.log(res)
  }).catch(err => {
    console.log('更新失败',err)//失败提示错误信息
  })
},

基本内容如上,参考我的数据库字段记录
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值