云函数:
今天记录一下关于云函数的调用问题,对于云函数的调用,我们首先要在云函数的文件的夹中新建Node.js云函数,然后编写对应的云函数,最后上传更新(创建并部署)。如图:

函数操作示例:
1.调用云函数sum
sum文件夹下的index.js中云函数编
// 云函数入口函数
exports.main = async(event, context) => {
return {
sum: event.a + event.b
}
}
云函数调用
/**
* 求和
*/
sum: function() {
wx.cloud.callFunction({
name: 'sum',
data: {
a: 21,
b: 32
}
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
},
2.获取openid
这里需要说一下,openID的获取,是login函数中的,在创建项目时,自带的一个,因此不需要我们编写,直接调用即可
/**
* 获取openid
*/
getOpenId: function() {
wx.cloud.callFunction({
name: 'login'
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
},
3.批量删除,云数据库的操作不可以直接对数据库进行批量删除,需要调用云函数。
批量删除的云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async(event, context) => {
try{
return await db.collection('user').where({
name: 'jerry'
}).remove()
}catch(e){
console.error(e)
}
}
网友评论