0
点赞
收藏
分享

微信扫一扫

unicloud进阶篇-云函数与云对象的区别


前面我们使用云函数方式,开发了一个小案例,现在我们继续学习unicloud,先看看云函数与云对象的区别。

unicloud进阶篇-云函数与云对象的区别_前端

在cloudfunctions上,右键新建云函数/云对象,

unicloud进阶篇-云函数与云对象的区别_mysql_02

 新建云对象,然后编写代码,方式与云函数差不多,

unicloud进阶篇-云函数与云对象的区别_获取数据_03

'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	let {num} = event;
	let res = await db.collection("article").limit(num).get();
	return res;
};

unicloud进阶篇-云函数与云对象的区别_数据库_04

// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
module.exports = {
	_before: function () { // 通用预处理器

	},
	async get(num){
		return await db.collection("article").limit(num).get();
	}
}

 pages index index.vue

<template>
	<view class="">
		
	</view>
</template>

<script>
	const cloudObj1 = uniCloud.importObject("cloudObj1");
	
	export default {
		data() {
			return {
				
			}
		},
		onLoad() {
			this.getData();
			this.getData2();
		},
		methods: {
			//云对象获取数据 
			getData2(){
				cloudObj1.get(3).then(res=>{
					console.log(res);
				})
			},
			//云函数获取数据
			getData(){
				uniCloud.callFunction({
					name:"cloudFunc1",
					data:{
						num: 2
					}
				}).then(res=>{
					console.log(res)
				})
			}
		}
	}
</script>

<style>
	
</style>

图示

unicloud进阶篇-云函数与云对象的区别_获取数据_05

举报

相关推荐

0 条评论