uni-app及uniCloud开发

uni-app及uniCloud开发

uniCloud后台

uniCloud后台

7、uniCloud云函数的入参

官网指导

'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
   
   //post 的参数在body里
   let data = event.body;
   let is_undefined = typeof(data) == 'undefined' ;
   let is_null = !data && typeof(data) !== 'undefined' && data != 0
   let is_success = false ;
   if(!(is_undefined || is_null)){
   	data = JSON.parse(data);
   	const collection = db.collection(data.colName || 'other');
   	let res = await collection.add(data.data) ;
   	if(res.id||res.ids){
   		is_success = true ;
   	}
   	
   }
   
   return {
   	success : is_success ,
   }
}

1、插入数据

user

"id":1,
"nickname":'昵称',
"gender":'性别或未知',
"language": "语言",
"city": "城市",
"province": "省份",
"country": "国家",
"avatarUrl": "头像",
"openId": "用户唯一标识",
"people": "666"
"time":"2021-06-30 00:22:22"

options

"id":1,
"content":"请假",
"is_other":1,
"id_user":1,
"time":"2021-06-30 00:22:22"
"id":2,
"content":"报名",
"is_other":1,
"id_user":1,
"time":"2021-06-30 00:22:22"
"id":3,
"content":"会晚10分钟到",
"is_other":1,
"id_user":1,
"time":"2021-06-30 00:22:22"

notify_options

"id_notify":1,
"id_options":1,
"time":"2021-06-30 00:22:22"

notify

"id":1,
"title":'标题',
"content":"文章内容,有以下选项",
"people":1,
"id_user":1,
"time":"2021-06-30 00:22:22"

participate

"id":1,
"id_user":1,
"id_options":1,
"id_notify":1,
"participants"'参与者',
"time":"2021-06-30 00:22:22"

notify_temp

"id":1,
"title":'标题',
"content":"文章内容,有以下选项",

"time":"2021-06-30 00:22:22"

2、uniCloud插入数据

2.1新建云函数add

'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
  // 获取 `user` 集合的引用
  const collection = db.collection('user');
  const res = await collection.add(event) ;
  return res
};

2.2 选中云函数,右键,上传并运行

2.3 前端页面调用, list是前端数据

addFun(){
  		let that = this ;
  		let data = that.list ;
  		uniCloud.callFunction({
  			name:'add',
  			data:data ,
  		})
  	},

3、uniCloud读取数据

3.1 带参

3.1.1新建云函数get
'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
  // 获取 `user` 集合的引用
  const collection = db.collection('user');
  const res = await collection.orderBy('_id','asc').get() ;
  return res;
};

3.1.2 选中云函数,右键,上传并运行
3.1.3 前端页面调用, list是前端数据
getFun(){
   		let that = this ;
   		uniCloud.callFunction({
   			name:'get',
   		}).then((res)=>{
   			that.list  = res.result.data ;
   		})
   	},

先不做分页

不带参

4、uniCloud删除数据

4.1新建云函数remove

'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
  db.collection('user').where({_id:event.id}).remove()
};

4.2 选中云函数,右键,上传并运行

4.3 前端页面调用, id是list带的前端数据

removeFun(id){
  		let that = this ;
  		uniCloud.callFunction({
  			name:'remove',
  			data:id ,
  		}).then((res)=>{
  			that.getFun() ;//刷新页面
  		})
  	},

5、uniCloud修改数据


6、配置白名单

微信小程序配置白名单

解决uniCloud测试环境能调云函数,正式环境不行的问题

在这里插入图片描述

api.bspapp.com
bsppub.oss-cn-shanghai.aliyuncs.com
tcb-api.tencentcloudapi.com
cos.ap-shanghai.myqcloud.com

有疑问

微信小程序联系客服,及时沟通
地图轨迹预览效果

扫描公众号,了解更多实例与资源免费分享:
实例分享

参考

前辈传授及官方指南摸索所得
参考1

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wx_h13813744

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值