天天看点

uni-app 云函数使用阿里云入门云函数的使用 阿里云

HBuilderX uni-app

  • 云函数的使用 阿里云
    • 代码实现
    • 逻辑调用

云函数的使用 阿里云

运行 在云端(服务器端) 的函数

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>