微信小程序云开发 删除用户信息

本文介绍如何使用微信开发者工具及云开发功能,在微信小程序中实现根据用户名查询并删除指定用户信息的方法。通过具体实例和代码展示整个过程。

编写一个微信小程序删除已注册的用户的信息。

开发平台:微信开发者工具
dele.png

微信云开发删除信息的代码字段remove:
delete.png

  1. 在delete.js页面添加数据库peocollection(需要提前在云数据库创建并添加一定数据)
    updp.png

  2. 按下按钮查找张三的_id号
    findz.png

  3. 在数据库中根据_id号进行查找,找到张三并删除其数据
    idde.png

修改后张三的数据已经删除

su.png

本次导入了vant-weapp库

可在该网站进行下载:
https://github.com/youzan/vant-weapp

####完整代码:

delete.js
const db = wx.cloud.database()
const peocollection = db.collection('people')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      page:0,
      id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      peocollection.get().then(res=>{
        console.log(res)
        this.setData({
          people: res.data
        })
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    peocollection.get().then(res=>{
        console.log(res)
        this.setData({
          people: res.data
        },res=>{
          console.log("数据更新完成")
          wx.stopPullDownRefresh()
        })
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page+25;

    peocollection.skip(page).get().then(res=>{
      let old_data=res.data
      let new_data=this.data.people
      this.setData({
        people: old_data.concat(new_data),
        page:page
      },res=>{
        console.log(res)
        //wx.stopPullDownRefresh()
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  click:function(event){
    console.log(event.currentTarget.dataset.id)
    peocollection.doc(event.currentTarget.dataset.id).update({
      data:{
        view: _.inc(1)
      }
    })
  },
  deleteData(){
    peocollection.where({
      "title":"张三"
    }).get().then(res=>{
      console.log(res)
      this.setData({
        id:res.data[0]._id
      })
      console.log(this.data.id)
      peocollection.doc(this.data.id).remove()
      console.log("数据删除成功")
    })
    
  }
})
delete.json(需要导入vant-weapp库,不然无法正常显示)
{
  "usingComponents": {
    "van-card": "../../dist/card"
  },
  "enablePullDownRefresh":true,
  "onReachBottomDistance":25
}
delete.wxml
<button type="primary" bindtap="deleteData">删除张三信息</button>
<block wx:for="{{people}}">
<van-card
  num="2"
  price="{{item.view}}"
  desc="{{item.team}}"
  title="{{item.title}}"
  data-id="{{item._id}}"
  thumb="{{ imageURL }}"
  bindtap="click"
/>
</block>
delete.wxss
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值