当点击查询按钮时,console.log()打印的结果是正常的,但是在模拟器上显示的计数有时候是正常的,有时候显示的数据库的总条数而不是查询的计数,数据显示也是有时候正常,有时候显示的初始化的数据而不是查询后的数据,当多次切换选项卡时,更明显。这是什么问题?模拟器排版如图
WXML代码:
<view class="search-input-group">
<view class="input-group">
<input class="search-input" placeholder="出发地" value="{{startPointStr}}" bindinput="onStartPointEvent" />
<image class="thumbnail" src="../../images/return.png" bindtap="onImage" />
<input class="search-input" placeholder="终点" value="{{goalStr}}" bindinput="onGoalEvent" />
</view>
<button class="search-button" size="mini" bindtap="onchaxun">查询</button>
</view>
<segment-carpool items="{{title}}" result="{{result}}" binditemchengde="onItemChengdeEvent" defaultIndex="0">
<view slot="0" class="segment-page wehicle-page">
<block wx:if="{{isQuerying == true}}">
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
<block wx:else>
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
</view>
<view slot="1" class="segment-page people-page">
<block wx:if="{{isQuerying == true}}">
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
<block wx:else>
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
</view>
<view slot="2" class="segment-page train-page">
<block wx:if="{{isQuerying == true}}">
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
<block wx:else>
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
</view>
<view slot="3" class="segment-page money-page">
<block wx:if="{{isQuerying == true}}">
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
<block wx:else>
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
</view>
<view slot="4" class="segment-page money-page">
<block wx:if="{{isQuerying == true}}">
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
<block wx:else>
<view class="comment-title">全部信息(共有{{result}}条)</view>
<whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
</block>
</view>
</segment-carpool>
<loadingmore hasmore="{{hasmore}}"></loadingmore>
JS代码:
const db = wx.cloud.database();
const _ = db.command
Page({
/**
* 页面的初始数据
*/
data: {
title: ["全部", "我要找车", "我要找人", "我找货车", "我要找货"],
hasmore: false,
wehicles: '',
startPoint: '',
goal: '',
isQuerying: false,
currentTabIndex: '',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 获取选项卡
*/
onItemChengdeEvent(event) {
var index = event.detail.index;
this.setData({
currentTabIndex: index
});
this.lowdrelease(index);
this.onchaxun(index);
},
/**
* 获取数据库数据
*/
async lowdrelease(index, start = 0) {
// console.log("index下标值",index)
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;
}
// console.log('carpool条件',query)
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;
// console.log('获取数据库数据',wehicles)
// 统计符合条件的总数而不是所有数据
const countRes = await db.collection("wehicle").where(query).count();
// console.log('总计数',countRes)
// 合并当前获取的数据
const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles;
newWehicles.forEach((wehicle, index) => {
wehicle.create_time = wehicle.create_time.toString();
});
// const hasmore = false;
const hasmore = countRes.total > (that.data.wehicles.length + start);
that.setData({
wehicles: newWehicles,
hasmore: hasmore,
result: countRes.total
});
},
/**
* 获取起点输入信息
*/
onStartPointEvent(event) {
const that = this;
const startPoint = event.detail.value
// console.log('起点', startPoint);
that.setData({
startPoint: startPoint,
startPointStr: startPoint
})
},
/**
* 获取终点输入信息
*/
onGoalEvent(event) {
const that = this;
const goal = event.detail.value
that.setData({
goal: goal,
goalStr: goal
})
},
/**
* 切换输入框的内容
*/
onImage() {
const startPoint = this.data.startPoint;
const goal = this.data.goal;
this.setData({
startPoint: goal,
goal: startPoint,
startPointStr: goal,
goalStr: startPoint
})
},
/**
* 点击查询事件
*/
async onchaxun(index,start = 0) {
const that = this;
that.setData({
isQuerying: true,
})
const whereRes = {
startPoint: db.RegExp({ regexp: that.data.startPoint}),
goal: db.RegExp({ regexp: that.data.goal}),
}
console.log(whereRes)
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;
}
console.log(query)
let promise = db.collection("wehicle").where(_.and(whereRes,query))
if (start > 0) {
promise = promise.skip(start)
}
// 限制每次获取数据数量并排序
promise = promise.limit(2)
.orderBy("redden.reddenStartTime", 'desc')
.orderBy("topping.toppingStarttime", "desc")
.orderBy("create_time", "desc");
const listzerosRes = await promise.get();
const wehicles = listzerosRes.data;
console.log('获取数据库数据',wehicles)
// 统计符合条件的总数而不是所有数据
const countRes = await promise.count();
console.log('总计数',countRes)
// 合并当前获取的数据
const newWheres = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles;
newWheres.forEach((wehicle, index) => {
wehicle.create_time = wehicle.create_time.toString();
});
// const hasmore = false;
const hasmore = countRes.total > (that.data.wehicles.length + start);
that.setData({
startPointStr: '',
goalStr: '',
wehicles: newWheres,
hasmore: hasmore,
result:countRes.total
});
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
if (!this.data.isQuerying) {
this.lowdrelease(0);
} else {
this.onchaxun(0);
}
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
this.lowdrelease(0);
this.setData({
isQuerying: false
})
wx.stopPullDownRefresh();
},
/**
* 页面上拉触底事件的处理函数
*/
async onReachBottom() {
let hasmore = true;
if (this.data.wehicles.length == 0) {
hasmore = false
}
this.setData({
hasmore: hasmore
})
if (!this.data.isQuerying) {
const currentIndex = this.data.currentTabIndex;
this.lowdrelease(currentIndex, this.data.wehicles.length);
} else {
this.onchaxun(this.data.wehicles.length);
}
},
})