index.wxml
<button class="call" bindtap="callFunction">云函数--查看</button>
<button class="call" bindtap="addFunction">云函数--添加</button>
<button class="call" bindtap="delFunction">云函数--删除</button>
<button class="call" bindtap="updateFunction">云函数--更新</button>
index.js
Page({
//查询数据
callFunction:function(){
console.log("按下按钮")
wx.cloud.callFunction({
name:"querryData"
}).then(console.log)
},
//添加数据
addFunction:function(){
console.log("addButton is click")
wx.cloud.callFunction({
name: "addData"
}).then(console.log)
},
//删除数据
delFunction: function() {
console.log("delButton is click")
wx.cloud.callFunction({
name: "delData"
}).then(console.log)
},
//更新数据
updateFunction: function () {
console.log("updateButton is click")
wx.cloud.callFunction({
name: "updateData"
}).then(console.log)
}
})
对应四个云函数
querryData
// 云函数入口文件 初始化
const cloud = require('wx-server-sdk')
cloud.init()
let db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
//获取所有数据
return await db.collection('todos').get();
}
addData
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
//引用database
let db = cloud.database()
// 新增数据
exports.main = async (event, context) => {
return await db.collection("todos").add({
data:{
name: 'function',
description:"this is a demo"
}
})
}
delData
const cloud = require('wx-server-sdk')
cloud.init()
let db = cloud.database();
exports.main = async (event, context) => {
//doc查询id对应项 remove删除
return await db.collection("todos").doc("1acf1de95e466bd30fa40e0b74b0f909").remove();
}
updateData
const cloud = require('wx-server-sdk')
cloud.init()
let db = cloud.database()
//更新数据
exports.main = async (event, context) => {
return await db.collection("todos").doc('da51bd8c5e4665770fa064af1cf1ebea').update({
data:{
time:123,
price:321
}
})
}
网友评论