原来的代码是查询了5次才实现的效果,想优化代码,优化之后,遇到两个问题,1、上拉加载失效,加载不出数据。2、第二个问题是,总计数,当上拉加载后,计数被改了,显示的是数据库全部的数量,如图。求大神指点,是什么问题?
代码:
/**
* 获取数据库数据
*/
async lowdrelease(index,start = 0) {
const that = this;
let query = {};
switch(index){
case 0:
//查询全部数据
break;
case 1:
query = {carpool:'我要找人'};
break;
case 2:
query = {carpool:'我要找车'};
break;
case 3:
query = {carpool:'我找货车'};
break;
case 4:
query = {carpool:'我要找货'};
break
}
let promise = db.collection("wehicle").where(query);
if (start > 0) {
promise = promise.skip(start);
console.log(promise)
}
promise.limit(5)
.orderBy("redden.reddenStartTime",'desc')
.orderBy("topping.toppingStarttime","desc")
.orderBy("create_time", "desc")
.get().then(res => {
console.log(res);
const wehicles = res.data;
promise.count().then(res => {
that.setData({
result: res.total
});
});
let hasmore = false
let newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles;
newWehicles.forEach((wehicle, index) => {
wehicle.create_time = wehicle.create_time.toString();
});
that.setData({
wehicles:newWehicles,
hasmore:hasmore,
})
})
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
let hasmore = true;
if(this.data.wehicles.length == 0){
hasmore = false
}
this.setData({
hasmore:hasmore
})
if(!this.data.isQuerying) {
this.lowdrelease(this.data.wehicles.length);
}else{
this.onchaxun(this.data.listzeros.length);
}
},
给你优化了一下
async lowdrelease(index, start = 0) { const that = this; let query = {}; switch (index) { case 0: break; case 1: query = { carpool: '我要找人' }; break; case 2: query = { carpool: '我要找车' }; break; case 3: query = { carpool: '我找货车' }; break; case 4: query = { carpool: '我要找货' }; break; } let promise = db.collection("wehicle").where(query); if (start > 0) { promise = promise.skip(start); } // 限制每次获取数据数量并排序 promise = promise.limit(5) .orderBy("redden.reddenStartTime", 'desc') .orderBy("topping.toppingStarttime", "desc") .orderBy("create_time", "desc"); const wehiclesRes = await promise.get(); const wehicles = wehiclesRes.data; // 统计符合条件的总数而不是所有数据 const countRes = await db.collection("wehicle").where(query).count(); // 合并当前获取的数据 const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles; that.setData({ wehicles: newWehicles, hasmore: wehicles.length === 5, result: countRes.total }); }