uni-app 字节小程序 unicloud-db联表查询问题:多个临时表组成的数组方法会报错,而多个collection字符串拼接不会报错。
uni-app 字节小程序 unicloud-db联表查询问题:多个临时表组成的数组方法会报错,而多个collection字符串拼接不会报错。
示例代码:
<template>
<!--不报错 -->
<!-- <unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="book,author" where="name == '水浒传'"> -->
<!--报错版本 -->
<unicloud-db v-slot:default="{data, loading, error, options}" :collection="colList">
<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('book').where('name == "水浒传"').getTemp(),
db.collection('author').getTemp()
]
}
},
onReady() {},
methods: {}
}
</script>
操作步骤:
只要使用上述代码即可
预期结果:
不报错
实际结果:
**mp.runtime.esm.js?66fd:5634 TypeError: Cannot read property '0' of undefined**
at a.mainCollection (unicloud-db.vue?5078:170)
at Pr.get (mp.runtime.esm.js?66fd:4419)
at Pr.evaluate (mp.runtime.esm.js?66fd:4524)
at a.eval [as mainCollection] (mp.runtime.esm.js?66fd:4799)
at eval (mp.runtime.esm.js?66fd:5596)
at Array.reduce (<anonymous>)
at bo (mp.runtime.esm.js?66fd:5595)
at a.wo [as __patch__] (mp.runtime.esm.js?66fd:5632)
at a.e._update (mp.runtime.esm.js?66fd:3956)
at a.r (mp.runtime.esm.js?66fd:5700)
at Pr.get (mp.runtime.esm.js?66fd:4419)
at new Pr (mp.runtime.esm.js?66fd:4408)
at xo (mp.runtime.esm.js?66fd:5706)
at a.Gr.$mount (mp.runtime.esm.js?66fd:6039)
at Object.__lifetimes_attached (index.js?f266:2336)
at Object.r.lifetimes.attached (index.js?f266:2341)
wo @ mp.runtime.esm.js?66fd:5634
e._update @ mp.runtime.esm.js?66fd:3956
r @ mp.runtime.esm.js?66fd:5700
Pr.get @ mp.runtime.esm.js?66fd:4419
Pr @ mp.runtime.esm.js?66fd:4408
xo @ mp.runtime.esm.js?66fd:5706
Gr.$mount @ mp.runtime.esm.js?66fd:6039
__lifetimes_attached @ index.js?f266:2336
r.lifetimes.attached @ index.js?f266:2341
t.tryCatch @ VM138:16
eval @ VM138:16
n.callComponentLifetime @ VM138:16
n.attached @ VM138:16
eval @ VM138:16
qc.emit @ VM138:2
eval @ VM138:2
subscribeHandler @ VM138:2
invokeService @ C:\Program Files\@bytedminiprogram-ide\resources\app\extensions\miniprogram-extension\node_modules\@byted\miniprogram-simulator\public\shell\preload\bgWebviewPreload.js:129
(anonymous) @ C:\Program Files\@bytedminiprogram-ide\resources\app\extensions\miniprogram-extension\node_modules\@byted\miniprogram-simulator\public\shell\preload\bgWebviewPreload.js:432
(anonymous) @ C:\Program Files\@bytedminiprogram-ide\resources\app\extensions\miniprogram-extension\node_modules\@byted\miniprogram-simulator\public\shell\preload\bgWebviewPreload.js:1
emit @ events.js:327
onMessage @ electron/js2c/renderer_init.js:91
appInit.js:73 onDBError {code: "SYNTAX_ERROR", message: "查询语句存在错误,请检查传入的临时表是否正确"}
appInit.js:78 SYNTAX_ERROR 查询语句存在错误,请检查传入的临时表是否正确
f @ appInit.js:78
eval @ index.js?a9ff:1
value @ index.js?a9ff:1
eval @ index.js?a9ff:1
unicloud-db.vue?5078:561 Error: 查询语句存在错误,请检查传入的临时表是否正确
at eval (index.js?a9ff:1)
bug描述:
unicloud-db使用:collection="colList"报错,代码具体见代码示例。
环境 | 版本 | 创建方式 |
---|---|---|
WIN10 | 字节小程序开发工具版本3.2.7-1 | HBuilder 3.4.7.20220422 |
3 回复
未能复现,贴一下你使用的schema文件,另外可以试试升级到HBuilderX 2.4.8看看有无报错
在使用 uni-app 开发字节小程序时,如果你使用 unicloud-db
进行联表查询,可能会遇到一些关于临时表和字符串拼接的问题。具体来说,你提到使用多个临时表组成的数组方法会报错,而使用多个 collection
字符串拼接不会报错。以下是一些可能的原因和解决方案:
1. 临时表数组方法报错的原因
unicloud-db
的联表查询通常使用 lookup
方法来实现。lookup
方法允许你指定多个表进行联表查询。然而,如果你使用临时表数组的方式,可能会遇到以下问题:
- 临时表未正确创建或引用:临时表需要在查询之前正确创建,并且在
lookup
方法中正确引用。如果临时表未正确创建或引用,可能会导致查询失败。 - 语法问题:
lookup
方法的语法可能不支持直接使用临时表数组。你可能需要将临时表转换为字符串或其他格式。
2. 字符串拼接方法不报错的原因
使用字符串拼接的方式通常不会报错,因为字符串拼接后的结果是一个合法的 collection
名称,unicloud-db
可以正确解析并执行查询。
3. 解决方案
如果你希望使用临时表数组进行联表查询,可以尝试以下方法:
方法一:使用字符串拼接
将临时表名称拼接成一个字符串,然后在 lookup
方法中使用该字符串。
const tempTable1 = 'temp_table_1';
const tempTable2 = 'temp_table_2';
const collection = `${tempTable1},${tempTable2}`;
this.db.collection('main_table')
.lookup({
from: collection,
localField: 'field_in_main_table',
foreignField: 'field_in_temp_table',
as: 'result'
})
.get()
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
方法二:逐个添加 lookup
如果你需要联表查询多个临时表,可以逐个添加 lookup
方法。
this.db.collection('main_table')
.lookup({
from: 'temp_table_1',
localField: 'field_in_main_table',
foreignField: 'field_in_temp_table_1',
as: 'result_1'
})
.lookup({
from: 'temp_table_2',
localField: 'field_in_main_table',
foreignField: 'field_in_temp_table_2',
as: 'result_2'
})
.get()
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
方法三:使用 aggregate
方法
如果你需要更复杂的联表查询,可以考虑使用 aggregate
方法,它提供了更灵活的查询方式。
this.db.collection('main_table')
.aggregate()
.lookup({
from: 'temp_table_1',
localField: 'field_in_main_table',
foreignField: 'field_in_temp_table_1',
as: 'result_1'
})
.lookup({
from: 'temp_table_2',
localField: 'field_in_main_table',
foreignField: 'field_in_temp_table_2',
as: 'result_2'
})
.end()
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});