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
};

 

posted @ 2021-07-15 15:03  YoYo/切克闹  阅读(2856)  评论(0编辑  收藏  举报