云函数

本文介绍了如何在uni-app中开始使用云函数,包括云数据库的添加、删除、更新和查找操作。在更新部分强调了update函数只能修改已存在数据,而set函数则能处理数据的添加或修改。在查找部分提到了条件查询和变量查询。
摘要由CSDN通过智能技术生成

开始使用云函数

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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JvidZtrZ-1612678269486)(F:\Uni-app.assets\image-20210207103627410.png)]

删除

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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iSm57oPf-1612678269489)(F:\Uni-app.assets\image-20210207104211984.png)]

数据库的更新和查找

更新

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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wFrA1ZtM-1612678269492)(F:\Uni-app.assets\image-20210207104757293.png)]

注意
  • 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)]

条件查询

'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
	}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值