uni-app unicloud客户端查询语句执行出错

uni-app unicloud客户端查询语句执行出错

产品分类:uniCloud/App

示例代码:

<template>
  <view class="container">
    <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}"
      where="sWhere" loadtime="manual"
      collection="collectionList" field="actionname,icon,iconcolor,description,limited_time,limited_time_ring,need_vibison,timeup_continue,in_interval_time,in_interval_ring,need_in_vibison,out_interval_time,out_interval_ring,need_out_vibison,create_date,user_id{_id,nickname}">
      <view v-if="error">{{error.message}}</view>
      <view v-else-if="data">
        <uni-list>
          <uni-list-item v-for="(item, index) in data" :key="index" showArrow :clickable="true" @click="handleItemClick(item._id)">
            <template v-slot:header>
              <uv-icon :name="item.icon" :color="item.iconcolor" size="28"></uv-icon>
            </template>
            <template v-slot:body>
              <view class="main">
                <text class="title">{{item.actionname}}</text>
                <view class="info">
                  <text class="author">{{item.user_id[0]?item.user_id[0].nickname:''}}</text>
                  <uni-dateformat class="last_modify_date" :date="item.create_date" format="yyyy-MM-dd" :threshold="[60000, 2592000000]" />
                </view>
              </view>
            </template>
          </uni-list-item>
        </uni-list>
      </view>
      <uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
    </unicloud-db>
    <uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" />
  </view>
</template>

<script>
const db = uniCloud.database()
export default {
  data() {
    return {
      collectionList: "kungfu-actions-diy,uni-id-users",
      loadMore: {
        contentdown: '',
        contentrefresh: '',
        contentnomore: ''
      },
      sWhere: "'hall_numb'==1 ||'icon'=='moments' || 'user_id'=='65f98a10a7c432936b30e275'"
    }
  },
  onPullDownRefresh() {
    this.$refs.udb.loadData({
      clear: true
    }, () => {
      uni.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    this.$refs.udb.loadMore()
  },
  onLoad() {
    console.log("onLoad", this.sWhere);
    this.$nextTick(() => {
      this.$refs.udb.loadData()
      console.log("loadData", this.$refs.udb.$data)
    })
  },
  methods: {
    handleItemClick(id) {
      uni.navigateTo({
        url: './detail?id=' + id
      })
    },
    fabClick() {
      uni.navigateTo({
        url: './addAction',
        events: {
          refreshData: () => {
            this.$refs.udb.loadData({
              clear: true
            })
          }
        }
      })
    }
  }
}
</script>

<style>
.main {
  justify-content: space-between;
  flex: 1;
}

.title {
  font-weight: bold;
  font-size: 32rpx !important;
  text-align: left;
}

.info {
  flex-direction: row;
  justify-content: space-between;
}

.author,
.last_modify_date {
  font-size: 14px;
  color: #999999;
}
</style>

操作步骤:

数据库

{
  "_id": "6617eefea09a9b12d7860da8",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "arrow-up-fill",
  "iconcolor": "#ff0000",
  "hall_numb": 0,
  "limited_time": 10,
  "need_vibison": false,
  "timeup_continue": false,
  "in_interval_time": 0,
  "need_in_vibison": false,
  "out_interval_time": 0,
  "need_out_vibison": false,
  "actionname": "我的那个去",
  "description": "啊发生的af\n阿法撒旦萨芬\n是否达到更高\nsgfsg\ngfsdfgdfgdsg岁的法国",
  "limited_time_ring": "",
  "in_interval_ring": "",
  "out_interval_ring": "",
  "create_date": 1712844542875
}
{
  "_id": "6617fd297ad52dfcccfc68a8",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "arrow-down-fill",
  "iconcolor": "#00ffff",
  "hall_numb": 2,
  "limited_time": 10,
  "need_vibison": false,
  "timeup_continue": false,
  "in_interval_time": 0,
  "need_in_vibison": false,
  "out_interval_time": 0,
  "need_out_vibison": false,
  "actionname": "测试",
  "description": "以仰卧式为主",
  "limited_time_ring": "",
  "in_interval_ring": "",
  "out_interval_ring": "",
  "create_date": 1712848169003
}
{
  "_id": "66180c878b0da4a4e4c98452",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "level",
  "iconcolor": "#ff0000",
  "hall_numb": 0,
  "limited_time": 10,
  "need_vibison": false,
  "timeup_continue": false,
  "in_interval_time": 0,
  "need_in_vibison": false,
  "out_interval_time": 0,
  "need_out_vibison": false,
  "actionname": "测试",
  "description": "whats up",
  "create_date": 1712852103133
}
{
  "_id": "66181031a09a9b12d78ce4db",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "gift-fill",
  "iconcolor": "#ff0000",
  "hall_numb": 0,
  "limited_time": 10,
  "need_vibison": false,
  "timeup_continue": false,
  "in_interval_time": 0,
  "need_in_vibison": false,
  "out_interval_time": 0,
  "need_out_vibison": false,
  "actionname": "33",
  "description": "33",
  "limited_time_ring": "",
  "in_interval_ring": "",
  "out_interval_ring": "",
  "create_date": 1712853041694
}
{
  "_id": "6618115ae0ec199b188baf49",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "gift-fill",
  "iconcolor": "#ff0000",
  "hall_numb": 0,
  "limited_time": 30,
  "need_vibison": true,
  "timeup_continue": true,
  "in_interval_time": 10,
  "need_in_vibison": true,
  "out_interval_time": 1,
  "need_out_vibison": true,
  "actionname": "4",
  "description": "1",
  "limited_time_ring": "",
  "in_interval_ring": "",
  "out_interval_ring": "",
  "create_date": 1712853338062
}
{
  "_id": "66181dbb7ad52dfccc0260b4",
  "user_id": "65f98a10a7c432936b30e275",
  "icon": "map-fill",
  "iconcolor": "#aa55ff",
  "hall_numb": 0,
  "limited_time": 25,
  "need_vibison": true,
  "timeup_continue": true,
  "in_interval_time": 7,
  "need_in_vibison": false,
  "out_interval_time": 10,
  "need_out_vibison": false,
  "actionname": "气贯长虹",
  "description": "测试",
  "limited_time_ring": "",
  "in_interval_ring": "",
  "out_interval_ring": "",
  "create_date": 1712856507773
}

预期结果:

{
  "bsonType": "object",
  "permission": {
    "read": true,
    "create": "auth.uid != null",
    "update": "doc.user_id == auth.uid",
    "delete": "doc.user_id == auth.uid"
  },
  "required": [
    "actionname",
    "icon",
    "iconcolor",
    "description",
    "hall_numb",
    "limited_time",
    "timeup_continue",
    "in_interval_time",
    "out_interval_time"
  ],
  "properties": {
    "_id": {
      "description": "动作 ID,系统自动生成"
    },
    "user_id": {
      "bsonType": "string",
      "description": "动作设置作者ID, 参考<code>uni-id-users</code> 表",
      "foreignKey": "uni-id-users._id",
      "defaultValue": {
        "$env": "uid"
      }
    },
    "actionname": {
      "bsonType": "string",
      "title": "名称",
      "description": "练习的名称,最小颗粒度",
      "order": 1,
      "trim": "both"
    },
    "icon": {
      "bsonType": "string",
      "description": "图标地址或代码",
      "title": "图标",
      "order": 2,
      "defaultValue": "gift-fill",
      "trim": "both"
    },
    "iconcolor": {
      "bsonType": "string",
      "description": "图标颜色",
      "title": "图标颜色",
      "trim": "both",
      "defaultValue": "#ff0000",
      "order": 3
    },
    "description": {
      "bsonType": "string",
      "title": "描述",
      "description": "描述",
      "label": "描述",
      "trim": "right",
      "order": 4
    },
    "hall_numb": {
      "bsonType": "int",
      "title": "大厅序号",
      "description": "大厅序号:0 太和宫 1 真武殿  2 玄武殿",
      "defaultValue": 0,
      "enum": [
        {
          "value": 0,
          "text": "太和宫"
        },
        {
          "value": 1,
          "text": "真武殿"
        },
        {
          "value": 2,
          "text": "玄武殿"
        }
      ]
    },
    "limited_time": {
      "bsonType": "int",
      "title": "定时",
      "order": 5,
      "defaultValue": 10,
      "description": "0:不限,大于等于1为限制的时长(分钟)"
    },
    "limited_time_ring": {
      "bsonType": "string",
      "description": "定时铃声地址",
      "title": "定时铃声",
      "order": 6,
      "trim": "both"
    },
    "need_vibison": {
      "bsonType": "bool",
      "title": "是否振动",
      "order": 7,
      "defaultValue": false,
      "description": "是否振动"
    },
    "timeup_continue": {
      "bsonType": "bool",
      "title": "是否允许延长",
      "order": 8,
      "defaultValue": false,
      "description": "到期后是否允许延长"
    },
    "in_interval_time": {
      "bsonType": "int",
      "title": "时限内间隔报时",
      "order": 9,
      "defaultValue": 0,
      "description": "0:不间隔报时,大于等于1为间隔的时长(分钟)"
    },
    "in_interval_ring": {
      "bsonType": "string",
      "description": "间隔报时铃声地址",
      "title": "间隔报时铃声",
      "order": 10,
      "trim": "both"
    },
    "need_in_vibison": {
      "bsonType": "bool",
      "title": "间隔是否振动",
      "order": 11,
      "defaultValue": false,
      "description": "间隔是否振动"
    },
    "out_interval_time": {
      "bsonType": "int",
      "title": "时限外间隔报时",
      "order": 12,
      "defaultValue": 0,
      "description": "0:不间隔报时,大于等于1为间隔的时长(分钟)"
    },
    "out_interval_ring": {
      "bsonType": "string",
      "description": "间隔报时铃声地址",
      "title": "间隔报时铃声",
      "order": 13,
      "trim": "both"
    },
    "need_out_vibison": {
      "bsonType": "bool",
      "title": "间隔是否振动",
      "order": 14,
      "defaultValue": false,
      "description": "间隔是否振动"
    },
    "create_date": {
      "bsonType": "timestamp",
      "description": "创建时间",
      "forceDefaultValue": {
        "$env": "now"
      }
    }
  },
  "version": "0.0.1"
}

实际结果:

使用unicloud-db客户端组件,where语句查询自定义数据库时,只要涉及到user_id字段就会出错,||隔开user_id条件和其他条件,实际查询效果是&&的效果,单独把user_id字段拿出来查询,查不到数据,这个问题排查了一天,快疯了的感觉。


更多关于uni-app unicloud客户端查询语句执行出错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

我自己找到原因了,联表查询的外联字段不能放在where语句中

更多关于uni-app unicloud客户端查询语句执行出错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在使用 UniApp 和 UniCloud 进行开发时,客户端查询语句执行出错可能有多种原因。以下是一些常见的排查步骤和解决方案:

1. 检查网络连接

  • 确保客户端网络连接正常,能够访问 UniCloud 服务。

2. 检查 UniCloud 配置

  • 确保 uniCloud 已正确初始化,并且在 manifest.json 中配置了正确的 uniCloud 服务空间。
  • 检查 uniCloud 服务空间是否已正确部署。

3. 检查查询语句

  • 确保查询语句的语法正确。UniCloud 使用的是 MongoDB 的查询语法,可以参考 MongoDB 的官方文档。
  • 检查查询条件是否正确,例如字段名、操作符等。
const db = uniCloud.database();
db.collection('your_collection').where({
    field: 'value'
}).get().then(res => {
    console.log(res);
}).catch(err => {
    console.error(err);
});

4. 检查权限设置

  • 确保 UniCloud 数据库的权限设置允许客户端进行查询操作。可以在 UniCloud 控制台中检查集合的权限设置。

5. 检查云函数调用

  • 如果查询是通过云函数调用的,确保云函数已正确部署,并且在客户端正确调用。
uniCloud.callFunction({
    name: 'your_function',
    data: {
        field: 'value'
    }
}).then(res => {
    console.log(res);
}).catch(err => {
    console.error(err);
});
回到顶部