开始使用云函数
get_list
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
//返回数据给客户端
return {
code:200,
msg:event.name+"的年龄"+event.age,
context
}
};
index.vue
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<button @click="open">执行云函数</button>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
open(){
uniCloud.callFunction({
name:"get_list",
data:{
name:"liming",
age:18
},
success(res) {
console.log(res)
},
fail() {
}
})
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
云数据库的添加和删除
添加
get_list.vue
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
let res=await collection.add([
{name:'uni-app'},{name:'html',type:'前端'}
])
console.log("数据插入:")
console.log(JSON.stringify(res))
return {
}
};
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WCdU8m3F-1612678269483)(F:\Uni-app.assets\image-20210207103613412.png)]](https://i-blog.csdnimg.cn/blog_migrate/697cc3b7b9d9d0d9f740c78651e11e18.png#pic_center)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JvidZtrZ-1612678269486)(F:\Uni-app.assets\image-20210207103627410.png)]](https://i-blog.csdnimg.cn/blog_migrate/fff9d4533e8a1d56286e1d93b6575d60.png#pic_center)
删除
get_list
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
//添加
// let res=await collection.add([
// {name:'uni-app'},{name:'html',type:'前端'}
// ])
// console.log("数据插入:")
// console.log(JSON.stringify(res))
//删除-删除最后一条记录
const res= await collection.doc('601f51bb6cea450001f3c18f').remove()
console.log(JSON.stringify(res))
return {
}
};
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bRYRTdQQ-1612678269487)(F:\Uni-app.assets\image-20210207104159306.png)]](https://i-blog.csdnimg.cn/blog_migrate/4041d582435f663a74a2c445a03f998e.png#pic_center)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iSm57oPf-1612678269489)(F:\Uni-app.assets\image-20210207104211984.png)]](https://i-blog.csdnimg.cn/blog_migrate/eabad4de0a69d8e735fd652a45b585aa.png#pic_center)
数据库的更新和查找
更新
get_list
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
//添加
// let res=await collection.add([
// {name:'uni-app'},{name:'html',type:'前端'}
// ])
// console.log("数据插入:")
// console.log(JSON.stringify(res))
//删除-删除最后一条记录
// const res= await collection.doc('601f51bb6cea450001f3c18f').remove()
// console.log(JSON.stringify(res))
//更新一条记录 or set({})
const res= await collection.doc('601f45d2d6547d0001cae0c3').update({
name:'52Hz'
})
console.log(JSON.stringify(res))
return {
}
};
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5M6mrioV-1612678269490)(F:\Uni-app.assets\image-20210207104741902.png)]](https://i-blog.csdnimg.cn/blog_migrate/78d1c3e7430b8c1b9136aecfefe6be6f.png#pic_center)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wFrA1ZtM-1612678269492)(F:\Uni-app.assets\image-20210207104757293.png)]](https://i-blog.csdnimg.cn/blog_migrate/2189695408433cde12647e511e9de24f.png#pic_center)
注意
- update(只能修改已存在数据)
- set(如果数据不存在则添加,如果存在则修改)
查找
get_list
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
//添加
// let res=await collection.add([
// {name:'uni-app'},{name:'html',type:'前端'}
// ])
// console.log("数据插入:")
// console.log(JSON.stringify(res))
//删除-删除最后一条记录
// const res= await collection.doc('601f51bb6cea450001f3c18f').remove()
// console.log(JSON.stringify(res))
//更新一条记录
// const res= await collection.doc('601f45d2d6547d0001cae0c3').update({
// name:'52Hz'
// })
// console.log(JSON.stringify(res))
//查询一条记录
const res= await collection.doc('601f45d2d6547d0001cae0c3').get()
console.log(JSON.stringify(res))
return {
}
};
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yh10g05e-1612678269493)(F:\Uni-app.assets\image-20210207105608649.png)]](https://i-blog.csdnimg.cn/blog_migrate/3be0d2a31161bd79106c5118f3e5769c.png#pic_center)
条件查询
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
//添加
// let res=await collection.add([
// {name:'uni-app'},{name:'html',type:'前端'}
// ])
// console.log("数据插入:")
// console.log(JSON.stringify(res))
//删除-删除最后一条记录
// const res= await collection.doc('601f51bb6cea450001f3c18f').remove()
// console.log(JSON.stringify(res))
//更新一条记录
// const res= await collection.doc('601f45d2d6547d0001cae0c3').update({
// name:'52Hz'
// })
// console.log(JSON.stringify(res))
//查询一条记录
const res= await collection.where({
name:'52Hz'
}).get()
console.log(JSON.stringify(res));
return {
code:200,
msg:'查询成功',
data:res.data
}
};
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aWnaELwn-1612678269494)(F:\Uni-app.assets\image-20210207110255921.png)]
变量查询
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<button @click="open">执行云函数</button>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
open(){
uniCloud.callFunction({
name:"get_list",
data:{
name:"52Hz"
},
success(res) {
console.log(res)
},
fail() {
}
})
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;js
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
'use strict';
const db=uniCloud.database()
exports.main = async (event, context) => {
const collection=db.collection("user")
//添加
// let res=await collection.add([
// {name:'uni-app'},{name:'html',type:'前端'}
// ])
// console.log("数据插入:")
// console.log(JSON.stringify(res))
//删除-删除最后一条记录
// const res= await collection.doc('601f51bb6cea450001f3c18f').remove()
// console.log(JSON.stringify(res))
//更新一条记录
// const res= await collection.doc('601f45d2d6547d0001cae0c3').update({
// name:'52Hz'
// })
// console.log(JSON.stringify(res))
//查询一条记录
const res= await collection.where({
//设置变量查询
name:event.name
}).get()
js
console.log(JSON.stringify(res));
return {
code:200,
msg:'查询成功',
data:res.data
}
};
本文介绍了如何在uni-app中开始使用云函数,包括云数据库的添加、删除、更新和查找操作。在更新部分强调了update函数只能修改已存在数据,而set函数则能处理数据的添加或修改。在查找部分提到了条件查询和变量查询。

909

被折叠的 条评论
为什么被折叠?



