前端调用云函数
uniCloud.callFunction({
name:'cloudFunctionName',
data:{
name:'张三',
age:18
},
success: (res) => {
console.log('调用云函数cloudFunctionName成功',res);
}
})
云函数
'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = db.collection('user');
//增
const res = await collection.add({
name:'李四',
age:10
})
console.log(JSON.stringify(res))
const res = await collection.add([
{
name:'用数组'
},
{
name:'包裹对象'
},
{
name:'实现批量传'
}
])
//删
const res = await collection.doc('传个id参数').remove()
console.log(JSON.stringify(res))
//改
const res = await collection.doc('传个id参数').update({
name:'更新name'
})
const res = await collection.doc('传个id参数').set({
name:'set是有则更新,无则新增',
tips:'传入的id不存在时,用update无法更新数据,用set可以新增一条传入的id+要更新的数据组成的记录',
notes:'update只能更新存在的记录,set如果记录存在就更新,如果记录不存在就添加'
})
console.log(JSON.stringify(res))
//查
const res = await collection.doc('传个id参数').get()
const res = await collection.where({
// name:'要查询的字段',
name:event.name
}).get()
console.log(JSON.stringify(res))
//event为客户端上传的参数
console.log('event : ', event)
//context包含了调用信息和运行状态,获取每次调用的上下文
//返回数据给客户端
// return event
return {
code:200,
// msg:event.name+'的年龄是'+event.age,
msg:'返回提醒msg',
data:res.data,
context
}
};
上传图片到云存储空间
open(){
uni.chooseImage({
count:1,
success: (res) => {
const path = res.tempFilePaths[0]
uniCloud.uploadFile({
filePath:path,
cloudPath:'a.jpg',
success: (res) => {
this.src = res.fileID
}
})
}
})
},
openthis(){
let self = this
uni.chooseImage({
count:1,
success(res){
const path = res.tempFilePaths[0]
uniCloud.uploadFile({
filePath:path,
cloudPath:'a.jpg',
success(res){
self.src = res.fileID
}
})
}
})
},
deletecloudimage(){
uniCloud.deleteFile({
fileList:['需要删除的图片地址'],
success(res) {
console.log(res)
}
})
}