uni-app 调用云函数的增删改查及上传图片

调用云函数
uniCloud.callFunction({
			name:"login",  // 函数名
    		// data上传的参数
            data:{
                name:"李白",
				age:"111"
			},
			success(res) {
				console.log(res)
			}
		})
云函数向数据库添加数据
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合
	// 向集合添加数据
	let res = await collection.add({
		name:'迪迦'
	})
	console.log(JSON.pase(res))
	//返回数据给客户端
	return {context,event}
};
云函数删除数据库数据
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合
	// 删除集合数据 doc(删除数据的id)
	let res = await collection.doc('60bb19e01ccee700016dc473').remove()
	console.log(JSON.stringify(res))
	//返回数据给客户端
	// return {context,event}
};

云函数更新数据库数据update
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合

	// 更新集合数据 update doc(需要更新的数据id)
	let res = await collection.doc('60bb1b66e22fbe0001950c29').update({
		name:'贝利亚'
	})
	console.log(JSON.stringify(res))
	//返回数据给客户端
	// return {context,event}
};

云函数更新数据库数据set
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合
	// 更新集合数据 set
	let res = await collection.doc('545').set({
		name:'贝利亚'
	})
	console.log(JSON.stringify(res))
	//返回数据给客户端
	// return {context,event}
};

update和set区别
  • update 只更新记录存在的 不存在的更新失败
  • set 如果记录存在就更新, 不存在就添加
云函数查找数据 根据id
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合
	// 查找数据 doc (需要查找数据的id)
	let res = await collection.doc('60bb1b66e22fbe0001950c29').get()
	console.log(JSON.stringify(res))
	//返回数据给客户端
	// return {context,event}
};
云函数查找数据 自定义字段查找where
'use strict';
 // 数据库的引用
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('users') // 获取云数据库的集合
	// 查找数据 
	let res = await collection.where({
		name:'迪迦'
	}).get()
	console.log(JSON.stringify(res))
	//返回数据给客户端
	// return {context,event}
};

云函数上传图片
// 上传图片
			btn() {
			uni.chooseImage({
				count:1,
				success(res) {
					const tempFilePath = res.tempFilePaths[0]
					const tempCloudPath = res.tempFiles[0].name
					uniCloud.uploadFile({
						// 上传图片地址
						filePath:tempFilePath,
						// 上传图片名字
						cloudPath:tempCloudPath,
						success(res) {
							console.log(res)
						},
						fail(err) {
							console.log(err)
						}
					})
				},
				fail(err) {
					console.log(err)
				}
			})
			}
云函数删除图片(客户端)
// 删除图片
			del () {

				uniCloud.deleteFile({
                    // 需要删除图片的地址
					fileList:['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-96913a35-5dce-4204-a2b4-79f330cee03f/8a43f9e3-1f3c-48e3-aa77-8d8b48af9b3e.jpg'],
					success(res) {
						console.log(res)
					},
					fail(err) {
						console.log(err)
					}
				})

			}

注意:客户端删除照片可能会报错delete_file_no_permission提示没有权限

在云函数中删除就好了

云函数端

'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const res = await uniCloud.deleteFile({
		fileList:['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-96913a35-5dce-4204-a2b4-79f330cee03f/8a43f9e3-1f3c-48e3-aa77-8d8b48af9b3e.jpg'],
		success(res) {
			console.log(res)
		},
		fail(err) {
			console.log(err)
		}
	})
	console.log('event : ', event)
	
	//返回数据给客户端
	return {
		data:res
	}
};

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值