准備工作:
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 };