hws007
hws007
  • 发布:2022-03-19 01:35
  • 更新:2022-04-18 16:30
  • 阅读:291

关联表问题,有空的来帮看看

分类:uniCloud

关联表,collection指定为多个临时表组成的数组,指定foreignKey,然后field,报foreignKey位置顺序错误,位置怎么放都不对,不用fieid又可以,collection设置为字符串拼接(""opendb-mall-goods,opendb-mall-sku"),这样又可以

<template>  
  <unicloud-db v-slot:default="{data, loading, error, options}" :collection="colList"  
foreignKey="opendb-mall-goods._id"  
                    field="_id.ck_id,sum(_id.quantity) as quantity"  
 >  
    <view v-if="error">{{error.message}}</view>  
    <view v-else-if="loading">正在加载...</view>  
    <view v-else>  
      {{data}}  
    </view>  
  </unicloud-db>  
</template>  
<script>  
  const db = uniCloud.database()  
  export default {  
    data() {  
      return {  
        colList: [  
          db.collection('"opendb-mall-goods').getTemp(),  
          db.collection('opendb-mall-sku').getTemp()  
        ]  
      }  
    },  
    onReady() {},  
    methods: {}  
  }  
</script>
2022-03-19 01:35 负责人:DCloud_uniAD_HDX 分享
已邀请:
DCloud_uniAD_HDX

DCloud_uniAD_HDX

foreignKey // foreignKey自 HBuilderX 3.3.7版本支持

下个版本改进,当前版本解决方案

  1. 找到 HBuilderX/plugins/uniapp-cli/node_modules/@dcloudio/uni-cli-shared/components/unicloud-db.vue
  2. 找到getTemp方法,把 foreignKey 放到 collection 后面,参考下面的代码
getTemp (isTemp = true) {  
  /* eslint-disable no-undef */  
  let db = uniCloud.database(this.spaceInfo)  

  if (this.action) {  
    db = db.action(this.action)  
  }  

  db = db.collection(...this.collectionArgs)  

  if (this.foreignKey) {  
    db = db.foreignKey(this.foreignKey)  
  }  
  if (!(!this.where || !Object.keys(this.where).length)) {  
    db = db.where(this.where)  
  }  
  if (this.field) {  
    db = db.field(this.field)  
  }  
  if (this.groupby) {  
    db = db.groupBy(this.groupby)  
  }  
  if (this.groupField) {  
    db = db.groupField(this.groupField)  
  }  
  if (this.distinct === true) {  
    db = db.distinct()  
  }  
  if (this.orderby) {  
    db = db.orderBy(this.orderby)  
  }  

  const {  
    current,  
    size  
  } = this.paginationInternal  
  const getOptions = {}  
  if (this.getcount) {  
    getOptions.getCount = this.getcount  
  }  
  const treeOptions = {  
    limitLevel: this.limitlevel,  
    startWith: this.startwith  
  }  
  if (this.gettree) {  
    getOptions.getTree = treeOptions  
  }  
  if (this.gettreepath) {  
    getOptions.getTreePath = treeOptions  
  }  
  db = db.skip(size * (current - 1)).limit(size)  

  if (isTemp) {  
    db = db.getTemp(getOptions)  
    db.udb = this  
  } else {  
    db = db.get(getOptions)  
  }  

  return db  
},
  • hws007 (作者)

    谢谢!但好像改的那路径的文件,不行,

    我改了HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-cli-shared/uni-components/lib/unicloud-db/unicloud-db.vue这路径下的文件,就可以了

    2022-04-18 18:32

  • DCloud_uniAD_HDX

    回复 hws007: 不好意思忘记说vue3的目录了

    2022-04-19 17:28

  • hws007 (作者)

    回复 DCloud_uniAD_HDX: 好的,谢谢!搞定了

    2022-04-21 21:59

该问题目前已经被锁定, 无法添加新回复