uniCloud云开发 3云函数调用和云数据库

2025-10-27 13:44:29

1、在云数据库中创建一个 user 表

uniCloud云开发 3云函数调用和云数据库

2、编写云函数并且操作云数据库,在 Hbuilder X中新增的 testAdd 云函数添加代码

'use strict';

const db = uniCloud.database();

// 获取 `user` 集合的引用

const user = db.collection('user');

exports.main = async (event, context) => {

//返回数据给客户端

let res = await user.add({name: '张三'});

return res;

};

3、上传云函数并运行:

13:52:52.693 [云端运行:阿里云:test]云函数testAdd上传完成,开始运行...

13:53:13.965 [云端运行:阿里云:test]运行状态:成功

13:53:13.965 [云端运行:阿里云:test]返回结果:{"id":"60f7b63883c458000178c267"}

13:53:13.965 [云端运行:阿里云:test]运行日志:

4、代码中使用

let res = await uniCloud.callFunction({

name: 'testAdd',

data: {name: '张三'}

})

5、如下图我在 onload 方法中执行云函数,插入成功会返回id

uniCloud云开发 3云函数调用和云数据库

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢