微信小程序服务器数据删除,微信小程序云开发-数据库增删改查

1.新建项目时不使用云服务,

616a67411f0b

image.png

2.在app.js初始化项目

616a67411f0b

image.png

3.新建云函数

616a67411f0b

image.png

4.在服务器新建list

616a67411f0b

image.png

5云服务端clouefunctions右键下新建nide.js云函数,写上文件名,

616a67411f0b

image.png

在这里我命名为chaxun

616a67411f0b

image.png

6在chaxun文件下的index.js中

// 云函数入口文件

const cloud = require('wx-server-sdk')

cloud.init()

const db=cloud.database()

// 细节:init要在db之前

// 客户端获取db,就要加wx. 云函数端获取db,就不能加wx.

// 云函数入口函数

exports.main = async (event, context) => {

// 任何一个云函数文件,必须返回一个东西

// 在云函数中sonsole.log()是没有意义的

// 查询数据

return db.collection('todos').get().then(res => {

return res;

})

}

客户端的代码

1 wxml中

查询数据

插入数据

插入多条数据

删除一条数据

修改数据

2 js中连接数据库

// 连接数据库

const db = wx.cloud.database()

3.js中查数据库的函数**

// 点击按钮查询数据

searchDataFn(){

// 查询所有文件的方式

// 可写条件

db.collection('todos').get().then(res => {

// res.data 包含该记录的数据

console.log(res)

})

},

4.js中插入数据库的方法

// 插入数据

insertDataFn(){

db.collection('todos').add({

data: {

username: "李四",

age: 37

}

}).then(res=>{

console.log(res)

})

},

5.js中插入多条数据数据的方法

// 插入多条数据

addToMenu(id, bool) {

return new Promise((resolve, reject) => {

let db = wx.cloud.database()

db.collection('todos').add({

data: {

_id: id,

done: bool

}

}).then(res=>{

resolve(res);

})

})

},

// 公用的插入数据方法

insertTotal(username, age){

return new Promise((resolve, reject)=>{

db.collection('todos').add({

data: {

username: username,

age: age

}

}).then(res=>{

resolve(res)

})

})

},

// 插入多条数据

addMoreDataFn(){

let _this = this;

async function fn(){

await _this.insertTotal("Lucy", 18);

await _this.insertTotal("Jack", 20);

await _this.insertTotal("龙哥", 18);

}

// 调用异步方法

fn();

},

6删除数据

// 删除数据

removeDataFn(){

db.collection('todos').where({

username: "李四"

}).remove().then(res=>{

console.log(res)

})

// db.collection('test').doc('e373396c5f640cc50009918c49c99d36').remove().then(res=>{

// console.log(res)

// })

},

修改数据

// 修改数据

updateDataFn(){

db.collection('todos').where({

username: "龙哥"

}).update({

data: {

username: "小龙"

}

}).then(res=>{

console.log(res)

})

},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值