求解答:
打印结果:console.log()
一个有[{...}]一个是[]
bannerList和goodsList都有结果
但是在wxml里没有接收到goodsList
但是如果goodslist的数据不用for循环,直接用数据库查询返回的记过
// var goodsList = res.data
// resolve({data: goodsList})这样的数据 返回的话 xwml是可以接收,
怀疑是不是for循环push方法
h或者是异步的原因?
// 获取首页数据 banners 和 商品列表
that.start().then(res =>{
console.log(app.globalData.bannerList)
console.log(app.globalData.goodsList)
that.setData({
goodsList: app.globalData.goodsList,
bannersList: app.globalData.bannerList
});
})
start: async function(){
var that = this
var bannerList = await this.getBannerList()
app.globalData.bannerList = bannerList.data
await this.getCityName()
var cityName = wx.getStorageSync('cityName')
if(cityName == ""){
that.getCityName()
}
var goodsList = await this.getGoodsList(cityName)
app.globalData.goodsList = goodsList.data
},
// 分页获取商品列表
getGoodsList: async function(cityName){
// 获取数据库实例
const db = wx.cloud.database()
const MAX_LIMIT = 10
// 先取出集合记录总数
let count = await db.collection('goods').count()
var total = count.total
// 计算需分几次取
const batchTimes = Math.ceil(total / 10)
// 获取商品列表
var goodsList = [];
for (var i = 0; i < batchTimes; i++) {
db.collection('goods').where({
'city_name': cityName,
'status': GOODS_STATUS_UP,
'type': GOODS_TYPE_NORMAL
}).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get({
success: function(res) {
// var goodsList = res.data
// resolve({data: goodsList})
if(res.data != null){
for(var i = 0; i < res.data.length; i++){
goodsList.push(res.data[i])
}
}
}
})
}
return new Promise(function(resolve, reject){
resolve({data: goodsList})
})
},
for (var i = 0; i < batchTimes; i++) { ... var res = await db.collection('goods').where({ // 加 await ... }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get() // 不加 success if (res.data != null){ for (var j = 0; j < res.data.length; j++){ // 不要再用 i goodsList.push(res.data[j]) } } } return {data: goodsList} // 不需要 Promise
就是异步问题
不用for循环又无法实现分页