unicloud调用云函数及增删改查云数据库

uniCloud调用云函数

 uniCloud.callFunction({
 	name:"云函数名称",
 	data:{"数据"},
 	success(){
 		//成功
 	},
 	fail(){
 		//失败
 	}
 })

unCloud调用云数据库

const db = uniCloud.database();
exports.main = async (event, context) => {
	//获取集合/数据表
	const collection = db.collection('user');
	//添加数据 await代表同步运行
	const res = await collection.add([
		{
			name:"库兹马",
			age:"26",
			No:"0",
			team:"洛杉矶湖人"
		},
		{
			name:"戴维斯",
			age:"28",
			No:"3",
			team:"洛杉矶湖人"
		}
	])
	//删除数据
	const res = await collection.doc('60a6099cfa23870001718106').remove()
	// 更新数据 update或者set  set如果没找到id 则会新增一条数据
	const res = await collection.doc('60a609ad5b70b50001a6d897').update(
		{
			name:"科比",
			age:"20",
			No:"24",
			team:"洛杉矶湖人"
		}
	)
	//查询数据——根据id查找 doc只能应用于id
	const res = await collection.doc('60a609ad5b70b50001a6d897').get()
	console.log(JSON.stringify(res))
	//查询数据——根据条件查找 使用where
	const res = await collection.where({
		team:"洛杉矶湖人"
	}).get()
	console.log(JSON.stringify(res))
	//返回数据给客户端
	return {
		code:200,
		msg:'查询成功',
		data:res.data
	}
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值