zoukankan      html  css  js  c++  java
  • uniCloud 基础示例(增、删、改、查)

    准备工作:
    1.在uniCloud目录右键创建并关联服务空间
    2.在uniCloud/database目录内db_init.json上右键初始化云数据库
    3.在uniCloud/cloudfunctions目录右键选择“上传所有云函数”
    开始愉快的体验uniCloud吧!

    1.新增一条数据

    页面:

    <button type="primary" @click="add">新增一条数据</button>

    add函数:

    async add() {
        uni.showLoading({
            title: '处理中...'
        })
        return await uniCloud.callFunction({
            name: 'add',
            data: {
                name: 'DCloud',
                subType: 'uniCloud',
                createTime: Date.now()
            }
        }).then((res) => {
            uni.hideLoading()
            uni.showModal({
                content: `成功添加一条数据,文档id为:${res.result.id}`,
                showCancel: false
            })
            console.log(res)
            return res.result.id
        }).catch((err) => {
            uni.hideLoading()
            uni.showModal({
                content: `添加数据失败,错误信息为:${err.message}`,
                showCancel: false
            })
            console.error(err)
        })
    }

    云函数页面(uniCloud/uniCloud-aliyun/cloudfunctions/add/index.js)

    'use strict';
    const db = uniCloud.database()
    exports.main = async (event, context) => {
        const collection = db.collection('unicloud-test')
        const res = await collection.add(event)
        return res
    };

    2.删除一条数据

    页面:

    <button type="primary" @click="remove">删除一条数据</button>

    remove函数

    async remove() {
        uni.showLoading({
            title: '处理中...'
        })
        return await uniCloud.callFunction({
            name: 'remove'
        }).then((res) => {
            uni.hideLoading()
            uni.showModal({
                content: res.result.msg,
                showCancel: false
            })
            console.log(res)
            return res.result.msg
        }).catch((err) => {
            uni.hideLoading()
            uni.showModal({
                content: `删除失败,错误信息为:${err.message}`,
                showCancel: false
            })
            console.error(err)
        })
    }

    云函数(uniCloud/uniCloud-aliyun/cloudfunctions/remove/index.js)

    'use strict';
    const db = uniCloud.database()
    exports.main = async (event, context) => {
        const collection = db.collection('unicloud-test')
        const docList = await collection.limit(1).get()
        if (!docList.data || docList.data.length === 0) {
            return {
                status: -1,
                msg: '集合unicloud-test内没有数据'
            }
        }
        const res = await collection.doc(docList.data[0]._id).remove()
        if (res.deleted === 1) {
            return {
                status: 0,
                msg: '成功删除unicloud-test内第一条数据'
            }
        } else {
            return {
                status: -2,
                msg: '删除数据失败'
            }
        }
    };

    3.修改数据

    页面:

    <button type="primary" @click="update">修改数据</button>

    updata函数

    async update() {
        uni.showLoading({
            title: '处理中...'
        })
        return await uniCloud.callFunction({
            name: 'update',
            data: {
                name: 'DCloud',
                subType: 'html 5+',
                createTime: Date.now()
            }
        }).then((res) => {
            uni.hideLoading()
            uni.showModal({
                content: res.result.msg,
                showCancel: false
            })
            console.log(res)
            return res.result.msg
        }).catch((err) => {
            uni.hideLoading()
            uni.showModal({
                content: `更新操作执行失败,错误信息为:${err.message}`,
                showCancel: false
            })
            console.error(err)
        })
    }

    云函数(uniCloud/uniCloud-aliyun/cloudfunctions/update/index.js)

    'use strict';
    const db = uniCloud.database()
    exports.main = async (event, context) => {
        const collection = db.collection('unicloud-test')
        const docList = await collection.limit(1).get();
        if (!docList.data || docList.data.length === 0) {
            return {
                status: -1,
                msg: '集合unicloud-test内没有数据'
            }
        }
        const res = await collection.doc(docList.data[0]._id).update(event);
        if (res.updated === 1) {
            let result = Object.assign({}, {
                _id: docList.data[0]._id
            }, event)
            return {
                status: 0,
                msg: `集合第一条数据由${JSON.stringify(docList.data[0])}修改为${JSON.stringify(result)}`
            }
        } else {
            return {
                status: -1,
                msg: `集合unicloud-test内没有数据`
            }
        }
    };

    4.查询前10条数据

    页面:

    <button type="primary" @click="get">查询前10条数据</button>

    get函数

    async get() {
        uni.showLoading({
            title: '处理中...'
        })
        return await uniCloud.callFunction({
            name: 'get'
        }).then((res) => {
            uni.hideLoading()
            uni.showModal({
                content: `查询成功,获取数据列表为:${JSON.stringify(res.result.data)}`,
                showCancel: false
            })
            console.log(res)
            return res.result.data
        }).catch((err) => {
            uni.hideLoading()
            uni.showModal({
                content: `查询失败,错误信息为:${err.message}`,
                showCancel: false
            })
            console.error(err)
        })
    }

    云函数(uniCloud/uniCloud-aliyun/cloudfunctions/get/index.js)

    'use strict';
    const db = uniCloud.database()
    exports.main = async (event, context) => {
        const collection = db.collection('unicloud-test')
        const res = await collection.limit(10).get()
        return res
    };
  • 相关阅读:
    类风湿性关节炎患者肿瘤坏死因子拮抗剂应用和心血管疾病的风险:系统文献复习
    合用DMARDs对持续抗TNF治疗的类风湿关节炎患者的影响:来自英国风湿病生物制剂注册系统的结果
    依那西普治疗幼年型特发性关节炎患者的疗程和停药原因
    TNF治疗早期类风湿关节炎患者严重感染和恶性肿瘤的风险: 随机对照研究的荟萃分析
    生物制剂对银屑病关节炎中轴表现的疗效:对依那西普治疗的一组患者为期12个月的观察性研究
    中国银屑病患者中银屑病关节炎的患病率和特征
    TNF抑制剂在10例新近起病的难治性反应性关节炎患者中的安全性与疗效
    中信国健临床通讯2011年1月第2期目录
    甲氨蝶呤治疗早期未用过DMARD的类风湿性关节炎的疗效预测:来自SWEFOT试验开放期初期的结果
    TopCoder入门教程 sqybi完善版[转载]
  • 原文地址:https://www.cnblogs.com/helloyoyo/p/15015649.html
Copyright © 2011-2022 走看看