- <template>
- <view>
- <!-- collection 数据表名称 -->
- <!-- data 获得的数据 -->
- <unicloud-db v-slot:default="{data, loading, error, options}" collection="contacts">
- <view v-if="error">{{error.message}}</view>
- <view v-else>
- {{data}}
- </view>
- </unicloud-db>
- </view>
- </template>
-
- <script>
- export default {
- data() {
- return {
-
- };
- }
- }
- </script>
-
- <style lang="scss">
-
- </style>
在列表页面,如果想删除一个item,原本要做很多事:
为减少重复开发,unicloud-db组件
提供了remove方法,在列表渲染时绑定好index,直接调用remove方法即可一行代码完成上述5步。
- <template>
- <view>
- <!-- collection 数据表名称 -->
- <!-- data 获得的数据 -->
- <unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="contacts">
- <view v-if="error">{{error.message}}</view>
- <view v-else>
- <uni-list>
- <uni-list-item
- v-for="item in data" :key="item._id"
- :title="item.name"
- :note="item.phone"
- link
- @longpress.native="rmItem(item._id)"></uni-list-item>
- </uni-list>
- </view>
- </unicloud-db>
- </view>
- </template>
-
- <script>
- export default {
- methods:{
- // 删除
- rmItem(id){
- this.$refs.udb.remove(id)
- }
- }
- }
- </script>
-
打开数据表 删除权限, 上传DB到服务器, 删除成功
- <template>
- <view>
- <uni-easyinput v-model="item.name" placeholder="" />
- <uni-easyinput v-model="item.phone" placeholder="" />
- <button type="default" @click="submit">提交</button>
- </view>
- </template>
-
- <script>
- export default {
- data() {
- return {
- item:{
- "name":"",
- "phone":""
- }
- }
- },
- methods: {
- submit(){
- const db = uniCloud.database()
- db.collection('contacts').add(this.item).then(e=>{
- console.log('e')
- })
- }
- }
- }
- </script>
[本地调试]"VALIDATION_ERROR: 数据库验证失败:提交的字段[\"name\",\"phone\"]在本地数据表的schema文件中不存在"
完善表结构字段
[本地调试]"[clientDB请求]表名:contacts,返回数据:" {"code":0,"errCode":0,"message":"","errMsg":"","systemInfo":Array(0)}
更新数据库
- <template>
- <view>
- <uni-easyinput v-model="item.phone" placeholder="" />
- <button type="default" @click="submit">提交</button>
- </view>
- </template>
-
- <script>
- export default {
- data() {
- return {
- item:{
- "_id":"",
- "name":"",
- "phone":""
- }
- }
- },
- onLoad(item) {
- this.item = JSON.parse(item)
- },
- methods: {
- submit(){
- const db = uniCloud.database()
- let item = {...this.item}
- delete item._id
- db.collection('contacts').doc(this.item._id).update(this.item).then(e=>{
- console.log('e')
- })
- }
- }
- }
- </script>