uniCloud 基礎操作


使用雲函數對數據庫進行增刪改查

'use strict';

// 鏈接數據庫
const db = uniCloud.database()

// 對數據庫聚合操作
const $ = db.command.aggregate

// 運行 在雲端(服務器端)的函數

exports.main = async (event, context) => {

    // event為客戶端上傳的參數
    // context 包含了調用信息和運行狀態,獲取每次調用 的上下文

    // 獲取某個數據表中的數據集合引用
    const collection = db.collection('user') // 獲取 user 數據表數據集合引用

    // 新增單條數據 (傳入一個對象)
    // let res =collection.add({
    //     name:"nui-app"
    // })

    // // 新增多條數據(傳入數組)
    // let res = await collection.add([{
    //         name: 'uni'
    //     },
    //     {
    //         name: 'app'
    //     }
    // ])

    // console.log('新增數據',JSON.stringify(res))



    // // 刪除
    // const res =await collection.doc('60745cf0fe0f4500016070e4').remove()
    // console.log('刪除數據',JSON.stringify(res))



    // 更新 
    // // 方法一:update
    // const res =await collection.doc('60745cf0fe0f4500016070e3').update({
    //     name:'hello'
    // })

    // // 方法二:set
    // const res=await collection.doc('60745cf0fe0f4500016070e3').set({
    //     name:'hi'
    // })


    // console.log('更新數據',JSON.stringify(res))

    // update 只能更新存在的記錄
    // set 如果記錄存在就更新,如果不存在就添加



    // 查找
    // 方法一: 查找某一條數據 ( doc 只能夠根據 id 進行查找 )
    // const res=await collection.doc('60745cf0fe0f4500016070e3').get()

    // 方法二: where 指定查詢條件,返回帶新查詢條件的新的集合引用 
    // const res = await collection.where({
    //     name: 'zyj'
    // }).get()

    
    // orderBy 指定查詢排序條件 
    // const res = await collection.orderBy('createTime', 'desc').get()
    
    
    // 獲取表中的 100 條數據,結果為 json 格式
    // limit 指定查詢結果集數量上限 
    const res =await collection.limit(100).get()
    

    console.log('查找數據', JSON.stringify(res))



    //返回數據給客戶端
    return {
        code: 200,
        msg: '查詢成功',
        data: res.data
    }
};

 

雲數據庫,如圖:

 

將圖片(文件)上傳到雲存儲

 上傳圖片
<template>
    <view>
        <button class="btn" type="default" @click="upload">上傳圖片</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {}
        },
        methods: {
            // 上傳圖片
            upload() {
                // 從本地相冊選擇圖片或使用相機拍照 
                uni.chooseImage({
                    count: 1, //最多可以選擇的圖片張數,默認9
                    success(res) {
                        console.log(res); 

                        // tempFilePaths 圖片的本地臨時文件路徑列表
                        if (res.tempFilePaths.length > 0) {
                            uni.showLoading({
                                title: '上傳中...'
                            })

                            let filePath = res.tempFilePaths[0]
                            // callback方式 ,進行上傳操作
                            uniCloud.uploadFile({
                                filePath: filePath, //要上傳的文件對象
                                cloudPath: Date.now(), //保存在雲端的文件名,這里以時間戳命名
                                success(res) {
                                    let imageUrl = res.fileID //雲端返回的圖片地址
                                    uniCloud.callFunction({ //調用雲端函數,把圖片地址寫入表
                                        name: 'images', //雲函數名稱
                                        data: { //提交給雲端的數據
                                            imageUrl: imageUrl,
                                            createTime: Date.now()
                                        },
                                        success: (res) => {
                                            console.log('數據插入成功')
                                            console.log(res)
                                        },
                                        fail: (err) => {
                                            console.log(err)
                                        },
                                        complete: () => {}
                                    })
                                },
                                fail(err) {
                                    console.log(err)
                                },
                                complete() {
                                    // 隱藏 loading 提示框 
                                    uni.hideLoading()
                                }
                            });
                        }
                    }
                });
            }
        }
    }
</script>

 

刪除圖片
<template>
    <view>
        <button class="btn" type="default" @click="delImg">刪除圖片</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {}
        },
        methods: {
            // 刪除圖片
            delImg() {
                // deleteFile 刪除雲端文件 
                uniCloud.deleteFile({
                    fileList: [
                        'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2faa875e-33ce-404e-b19d-2be906006793/3375ca49-79ac-45d9-98dc-d8ff759e5873.jpg'
                    ], // 圖片路徑
                    success(res) {
                        console.log(res)
                    },
                    fail(err) {
                        console.log(err)
                    }
                })
            }
        }
    }
</script>

在客戶端刪除圖片時,會報錯(刪除文件報沒有權限):

Error: delete_file_no_permission https://vkceyugu.cdn.bspapp.com/VKCEYUGU-uni0d40711/9b454da9-a759-44ae-9a38-479961e04003.jpg
at Function.complete (pages-index-index.js:381)
at chunk-vendors.js:98
at Object.A [as callback] (chunk-vendors.js:98)
at m (chunk-vendors.js:98)
at XMLHttpRequest.w.onload (chunk-vendors.js:98)

官方介紹:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM