Add, delete, modify and check uniCloud cloud library and upload to cloud storage


Preface

本文介绍uniCloud创建云函数以及云对象的方法,以及数据的增删改查,云函数的调用,传值等,云储存上传资源


提示:以下是本篇文章正文内容,下面案例可供参考

Get a reference to a collection

Insert image description here

云函数
const db = uniCloud.database();
// 获取 `user` 集合的引用
exports.main = async (event, context) => {
    
    
const collection = db.collection('user');
}

使用
uniCloud.callFunction({
    
    
    name:"users"//云函数名
 })
云对象
const db = uniCloud.database();
module.exports = {
    
    
	_before: function () {
    
     // 通用预处理器
      
	},
	get(){
    
    
		db.collection('users').get()
	}
}
使用
//引入
const cloudObj=uniCloud.importObject();
cloudObj.get()

Collection

You can obtain the reference of the specified collection through db.collection(name), and you can perform the following operations on the collection

type interface illustrate
Write add Add new record (trigger request)
count count Get the number of records that meet the conditions
read get Get the records in the collection. If the query condition is defined using the where statement, the matching result set will be returned (triggering the request)
Quote doc Gets a reference to the record with the specified id in the collection
Query conditions where Filter matching records by specifying conditions, which can be used with query instructions (eq, gt, in, …)
skip Skip a specified number of documents, often used for paging, passing in offset
orderBy sort by
limit Limits on the returned result set (number of documents), with default and upper limit values
field Specify the fields to be returned

Query filtering instructions

type interface illustrate
comparison operation eq Field equals ==
neq Field is not equal to !=
gt Field is greater than >
gte Field greater than or equal to >=
lt Field is smaller than <
lte Field less than or equal to <=
in field value in array
of Field value is not in array
logic operation and Indicates that all specified conditions must be met at the same time
or Indicates that at least one of the specified conditions must be met at the same time

.collection cloud function

const  db = uniCoud.database()
exports.main = =async (event,context)=>{
    
    
   let {
    
    num} = =event;//callFunction传递的data数据
   return await db.conllection('users').get()
}

add new

// 单条插入数据
let res = await collection('users').add()//对象
// 批量插入数据
let res = await collection('users').add([])//数组

Query where

  • limit(1) returns one
  • skip(4) skips the specified position
  • orderBy(field, asc) sort //field field //asc ascending order/dasc descending order
  • field() query returns the specified field

let res = await db.collection('goods').limit(1).where({
    
    //返回一条数据
  category: 'computer',//符合此条件的表
   name: new RegExp('^ABC')//正则匹配
}).get()

skip(4)  跳过指定位置
let res = await collection('users').skip(4).get()

orderBy(field, asc) 排序  //field字段  //asc升序/dasc降序
let res = await collection.orderBy("name", "asc").get()

field() 查询返回指定的字段
collection.field({
    
     'age': true }) //只返回age字段、_id字段,其他字段不返回

removeremove()

collection.doc(_id).remove()

// 清理全部数据
let res = await collection('users').doc().remove();

//删除指定文档
let res = await collection('users').doc(document.id).remove();

updateupdata

When using Tencent Cloud, the update method must be used with the doc and where methods. db.collection('test').update() will report the following error: param should have required property 'query'

When the set and remove update operators are not used, this method will not delete the field, but only merge the updated data with the existing data.

Update object

//根据doc-id更新这条数据,注意doc-id不可出现在update对象里否者不生效
let res = await collection.doc('doc-id').update({
    
    
  name: "Hey",
  count: {
    
    
    fav: 1
  }
});

Update array

//直接数组下标传值
let res = await collection.doc('doc-id').update({
    
    
  arr: {
    
    
    1: "uniCloud"
  }
})


Update the document or create collection.doc().set() if it does not exist

This method will overwrite existing fields. Please note that it behaves differently from update. For example, in the following example, the follow field will be deleted after executing set.

let res = await collection.doc('doc-id').set({
    
    
  name: "Hey",
  count: {
    
    
    fav: 1
  }
})

Update documents in batches

const dbCmd = db.command
let res = await collection.where({
    
    name: dbCmd.eq('hey')}).update({
    
    
  age: 18,
})

Updates the element at the specified index in the array

const res = await db.collection('query').doc('1').update({
    
    
  // 更新students[1]
  ['students.' + 1]: {
    
    
    name: 'wang'
  }
})

Query command

const dbCmd = db.command
const myOpenID = "xxx"
let res = await db.collection('articles').where({
    
    
  quarter: dbCmd.eq('2020 Q2')
}).get()

uniCloud.callFunction()

Call cloud function

uniCloud.callFunction({
    
    
  name:"mydata"//云函数名称
  data:{
    
    
  num:“”
  }//传输的数据
})

cloud storage

When using cloud storage, you must keep HbuilderX to support this version of the uni component. Because I used HbuilderX half a year ago, the upload always showed that I have no permission. Please pay attention! !

Insert image description here
You can upload the cloud repository by directly referencing the component.

<uni-file-picker 
	v-model="imageValue" 
	fileMediatype="image" 
	mode="grid" 
	@select="select" 
	@progress="progress" 
	@success="success" 
	@fail="fail" 
/>
<script>
	export default {
    
    
		data() {
    
    
			return {
    
    
				imageValue:[]
			}
		},
		methods:{
    
    
			// 获取上传状态
			select(e){
    
    
				console.log('选择文件:',e)
			},
			// 获取上传进度
			progress(e){
    
    
				console.log('上传进度:',e)
			},
			
			// 上传成功
			success(e){
    
    
				console.log('上传成功')
			},
			
			// 上传失败
			fail(e){
    
    
				console.log('上传失败:',e)
			}
		}
	}
</script>


Refer to the official website documentation
and Xianxiami uniCloud video

Summarize

Tip: Here is a summary of the article:
Basic additions, deletions, and modifications to the uniCloud cloud library, as well as problems encountered. When uploading pictures, it directly displays 403 Not uploaded to the cloud storage by the whole school, which has been solved.

Guess you like

Origin blog.csdn.net/yang20000222/article/details/130889983