uni-app:web端和数据库关联(通过云函数)

  • Post author:
  • Post category:其他


uniCloud云数据库中创建表

  1. 创建一个云函数

  1. 在index.js中首先要连接云函数:const db=uniCloud.database();

  1. 选择数据库中的具体一张表:db.collection(”表名”);

  1. 通过接口获得get()获取集合中的数据;

  1. 最后return传数据;

云函数中代码如下:

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

接下来在页面中连接云函数:uni.callFunction()

页面连接云函数代码如下:

uniCloud.callFunction({
                name:"cloudDemo01",  //云函数名
                data:{}
            }).then(res=>{
                //console.log(res)
                this.userArr=res.result.data  //赋值
            })

* v-for渲染模板的时候注意:

key=”item._id”



版权声明:本文为m0_55700773原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。