Dexie.js筛选仅返回单个值问题排查求助
DexieDB查询仅返回第一条数据的问题分析
你的DexieDB结构
const db = new Dexie('shipment-execution'); db.version(1).stores({ root: '++id,shipmentid,stopid', })
数据库存储的数据
| id | shipmentid | stopid |
|---|---|---|
| 1 | 6000001 | 10 |
| 2 | 6000001 | 20 |
| 3 | 6000001 | 30 |
| 4 | 6000002 | 10 |
| 5 | 6000002 | 20 |
你的查询代码
db.root.where({shipmentid : '6000001'}).toArray().then((d)=>{ console.log(d); })
实际输出结果
| id | shipmentid | stopid |
|---|---|---|
| 1 | 6000001 | 10 |
错误原因
问题出在数据类型不匹配:你存储的shipmentid是数字类型(如6000001),但查询时使用了字符串类型的'6000001'。Dexie的查询遵循严格的类型匹配规则,只有当查询条件的类型与存储数据的类型完全一致时,才能匹配到所有符合条件的记录。当前查询只会匹配类型为字符串的shipmentid,导致仅返回一条结果。
修正后的查询代码
将查询条件中的字符串改为数字即可:
db.root.where({shipmentid : 6000001}).toArray().then((d)=>{ console.log(d); })
内容的提问来源于stack exchange,提问作者Badhusha
相关产品推荐
相关产品推荐

