新建一个uni-app 项目 启动云开发 选择想要的云服务
在次之前先完成uniCloud 的实名认证

新云函数 的基础样式

选择上传并运行 等待控制台上传完成

可以去web端 云函数列表查看 是否有云函数出现

在页面请求云函数
<template>
<view class="content">
<!-- 定义请求按钮 -->
<button type="default" @click="req">请求</button>
</view>
</template>
<script>
export default {
methods: {
//请求参数
request() {
//调取云函数
uniCloud.callFunction({
name: 'login', // 云函数名字
// 传输数据
data: {
name:'测试'
},
// 成功
success(res) {
console.log(res);
},
//失败
fail(e) {
console.log(e);
}
})
}
}
}
</script>
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
return {
code: 200,
msg: '查询成功',
data: event
}
}
成功样式

'use strict'; const db = uniCloud.database() //对数据库的对象获取; exports.main = async (event, context) => { const collection = db.collection('holle') //对holle数据库的获取; //event为客户端上传的参数 console.log('event : ', event) // 添加数据 let res = await collection.add({ name: event.name //data传过来的name }) return { code: 200, msg: '查询成功', data: res } }
添加一个对象数组 这边为了方便展示 就直接写死要传的值
'use strict';
const db = uniCloud.database() //对数据库的对象获取;
exports.main = async (event, context) => {
const collection = db.collection('holle') //对holle数据库的获取;
//event为客户端上传的参数
console.log('event : ', event)
// 添加对象数组
let res = await collection.add([{
name: '测试1'
}, {
name: '测试2',
tiem: Date.now() //获取当前时间
}])
return {
code: 200,
msg: '查询成功',
data: res
}
}
添加成功 去web端云数据库查看

'use strict'; const db = uniCloud.database() //对数据库的对象获取; exports.main = async (event, context) => { const collection = db.collection('holle') //对holle数据库的获取; //event为客户端上传的参数 console.log('event : ', event) // 删除数据 测试1 let res = await collection.doc('604ae3b233ae930001f67840').remove() console.log(JSON.stringify(res)); return { code: 200, msg: '查询成功', data: res } }

去web控制台刷新就发现 测试 1 的数据被删除了
'use strict'; const db = uniCloud.database() //对数据库的对象获取; exports.main = async (event, context) => { const collection = db.collection('holle') //对holle数据库的获取; //event为客户端上传的参数 console.log('event : ', event) // 修改数据 对测试 就行修改 let res = await collection.doc('604ae03e9e89280001740a76').update({ name: '现在修改 测试' }) console.log(JSON.stringify(res)); return { code: 200, msg: '查询成功', data: res } }
修改成功 去web端云数据库查看

'use strict'; const db = uniCloud.database() //对数据库的对象获取; exports.main = async (event, context) => { const collection = db.collection('holle') //对holle数据库的获取; //event为客户端上传的参数 console.log('event : ', event) // 数据更新 let res = await collection.doc('604ae03e9e89280001740a76').set({ name: '现在修改 测试', tiem: Date.now(), //获取当前时间 key: '更新出现的' }) console.log(JSON.stringify(res)); return { code: 200, msg: '查询成功', data: res } }
更新成功 去web端云数据库查看

'use strict'; const db = uniCloud.database() //对数据库的对象获取; exports.main = async (event, context) => { const collection = db.collection('holle') //对holle数据库的获取; //event为客户端上传的参数 console.log('event : ', event) // 查找数据 寻找key为 更新出现的 let res = await collection.where({ key: '更新出现的' }).get() console.log(JSON.stringify(res)); return { code: 200, msg: '查询成功', data: res } }
去页面点击按钮查看打印出来的数据

项目地址:https://gitee.com/jielov/uni-cloud_development





