在次以前先完成uniCloud 的實名認證 javascript
html
新雲函數 的基礎樣式java
選擇上傳並運行 等待控制檯上傳完成git
能夠去web端 雲函數列表查看 是否有云函數出現web
<template> <view class="content"> <!-- 定義請求按鈕 --> <button type="default" @click="req">請求</button> </view> </template> <script> export default { methods: { //請求參數 request() { //調取雲函數 uniCloud.callFunction({ name: 'login', // 雲函數名字 // 傳輸數據 data: { name:'測試' }, // 成功 success(res) { console.log(res); }, //失敗 fail(e) { console.log(e); } }) } } } </script>
數據庫
'use strict'; exports.main = async (event, context) => { //event爲客戶端上傳的參數 console.log('event : ', event) return { code: 200, msg: '查詢成功', data: event } }
成功樣式數組
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 添加數據 let res = await collection.add({ name: event.name //data傳過來的name }) return { code: 200, msg: '查詢成功', data: res } }
添加一個對象數組 這邊爲了方便展現 就直接寫死要傳的值 app
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 添加對象數組 let res = await collection.add([{ name: '測試1' }, { name: '測試2', tiem: Date.now() //獲取當前時間 }]) return { code: 200, msg: '查詢成功', data: res } }
添加成功 去web端雲數據庫查看函數
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 刪除數據 測試1 let res = await collection.doc('604ae3b233ae930001f67840').remove() console.log(JSON.stringify(res)); return { code: 200, msg: '查詢成功', data: res } }
去web控制檯刷新就發現 測試 1 的數據被刪除了
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 修改數據 對測試 就行修改 let res = await collection.doc('604ae03e9e89280001740a76').update({ name: '如今修改 測試' }) console.log(JSON.stringify(res)); return { code: 200, msg: '查詢成功', data: res } }
修改爲功 去web端雲數據庫查看
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 數據更新 let res = await collection.doc('604ae03e9e89280001740a76').set({ name: '如今修改 測試', tiem: Date.now(), //獲取當前時間 key: '更新出現的' }) console.log(JSON.stringify(res)); return { code: 200, msg: '查詢成功', data: res } }
更新成功 去web端雲數據庫查看
'use strict'; const db = uniCloud.database() //對數據庫的對象獲取; exports.main = async (event, context) => { const collection = db.collection('holle') //對holle數據庫的獲取; //event爲客戶端上傳的參數 console.log('event : ', event) // 查找數據 尋找key爲 更新出現的 let res = await collection.where({ key: '更新出現的' }).get() console.log(JSON.stringify(res)); return { code: 200, msg: '查詢成功', data: res } }
去頁面點擊按鈕查看打印出來的數據