幕布斯6574152
2019-12-05 00:04
//云函数BookingList
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const MAX_LIMIT = 100
exports.main = async (event, context) => {
// 先取出集合记录总数
const countResult = await db.collection('user').count()
const total = countResult.total
// 计算需分几次取
const batchTimes = Math.ceil(total / 100)
// 承载所有读操作的 promise 的数组
const tasks = []
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection('user').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
}
// 等待所有
return (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data),
// errMsg: acc.errMsg,
}
})
}
//'user'集合中有一个数据库为:
"XXXd81"
"_openid":
"XXXko"
"date":
"2019-12-02"
"region":
"广东省,XX市,XX区"
"Name":
"类型名称"
"rubbishWeight":
"8"
"totalPrice":
"3"
"userAdress":
"XX园"
"userMobile":
"189XXXXX"
"userName":
"姓名"
"way":
"晚上18:00-21:00"
// pages/shou/shou.js
const db = wx.cloud.database(); //初始化数据库
Page({
/**
* 页面的初始数据
*/
data: {
bookingArr: []
},
onLoad: function(options) {
// 调用云函数'BookingList;
wx.cloud.callFunction({
name: 'BookingList'
}).then(res => {
console.log('调用云函数BookingList的结果:', res)
this.setData({
bookingArr: res.result.data
})
}).catch(err => {
console.log('调用云函数BookingList失败', err)
})
},
<!--显示日期-->
<view class="booking" wx:for="{{bookingArr}} "wx:key="{{index }}" >日期: {{item.date}}</view>
页面是上面这样写的,但 {{item.date}}没有渲染出内容。
补充:如果增加一句打印显示bookingArr的语句“console.log('显示bookingArr的结果:', bookingArr)”在:
onLoad: function(options) {
// 调用云函数'BookingList;
wx.cloud.callFunction({
name: 'BookingList'
}).then(res => {
console.log('调用云函数BookingList的结果:', res)
this.setData({
bookingArr: res.result.data
})
console.log('显示bookingArr的结果:', bookingArr)
}).catch(err => {
console.log('调用云函数BookingList失败', err)
})
则会增加一条报错显示:
调用云函数BookingList失败 ReferenceError: bookingArr is not defined
at quanbudingdan.js? [sm]:52
补充:无论写成 bookingArr: res.result 还是 bookingArr: res.result.data,都没法渲染出来?
调试器下面显示的是:
调用云函数BookingList的结果: {errMsg: "cloud.callFunction:ok", result: {…}, requestID: "cd8408ce-1763-11ea-8bd4-525400a88e2e"}
展开此结果为:
{errMsg: "cloud.callFunction:ok", result: {…}, requestID: "38099390-1764-11ea-841d-525400b2c41b"}
errMsg: "cloud.callFunction:ok"
requestID: "38099390-1764-11ea-841d-525400b2c41b"
result:
data: Array(1)
0:
date: "2019-12-02"
region: "广东省,XX市,XX区"
rubbishName: "类型名称"
rubbishWeight: "8"
totalPrice: "3"
userAdress: "XX园"
userMobile: "189XXXXX"
userName: "姓名"
way: "晚上18:00-21:00"
_id: "XXXd81"
_openid: "XXXko"
__proto__: Object
length: 1
nv_length: (...)
__proto__: Array(0)
errMsg: "collection.get:ok"
__proto__: Object
__proto__: Object
我是不是因为下面这个赋值式子写错,但不知道如何修改才能在wxml正确渲染出来:
this.setData({
bookingArr: res.result
})
能否帮忙修改这个赋值式子?感谢!
首先看下调试器中输出的res内容是什么:

然后看下页面上显示bookingArr的地方是否有问题。
轻松入门微信小程序与云开发
64635 学习 · 1742 问题
相似问题