HBuilderX uni-app
- 雲函數的使用 阿裡雲
-
- 代碼實作
- 邏輯調用
雲函數的使用 阿裡雲
運作 在雲端(伺服器端) 的函數
代碼實作
'use strict';
//運作 在雲端(伺服器端) 的函數
//聚合
const db = uniCloud.database()
exports.main = async (event, context) => {
//event為用戶端上傳的參數
//context 包含了調用資訊和運作狀态 , 擷取每次調用的上下文
// console.log('event : ', event)
//得到集合的引用 進行增删改查
const collection = db.collection('user')
// console.log('資料插入')
// 同步結果 await 增加
// let res = await collection.add([
// {
// "name":'小月月'
// },
// {
// "name":'小張'
// }
// ])
// 同步結果 await 删除
// let res = await collection.doc('605ae9726e0c9a00019cff8c').remove()
// 同步結果 await 修改 set方法 存在修改 不存在新增 updata隻做修改
// let res = await collection.doc('605ad47ef5792e00019f66c4').set({
// "name":'小王八',
// "type":'千年的'
// })
// console.log(JSON.stringify(res))
//
//根據 id 查詢
// const res = await collection.doc('605ad47ef5792e00019f66c4').get()
//根據條件查詢
const res = await collection.where({
"name":event.name
}).get()
console.log(JSON.stringify(res))
//傳回資料給用戶端
return {
code:200,
msg:"查詢成功",
data:res.data
}
};
邏輯調用
<template>
<view class="content">
<image class="logo" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-3c709047-5684-428a-b98f-acfaeaa344dd/8928a212-edce-4a6d-b1bd-930f37936fa3.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:"小月月",
age:"26"
},
success(res) {
console.log(res)
},
fail(res) {
console.log(res)
}
})
}
}
}
</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>