//getFeedbackResult 云函数
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
const openid = cloud.getWXContext().OPENID;
const currentDate = new Date().toLocaleDateString();
return db.collection('us_feedback').where({
_openid: openid,
createdate: currentDate
}).get()
}
//feedback.js
//提交表单时方法
formSubmit(e) {
let feedbackDataNum = getFeedbackNum();
console.log('2、调用执行方法,并发返回结果', feedbackDataNum)
}
//调用云函数方法
function getFeedbackNum() {
const currentDate = new Date().toLocaleDateString();
let feedbackDataNum = -1;
wx.cloud.init();
wx.cloud.callFunction({
name: "getFeedbackResult",
data: {}
}).then(res => {
console.log('1、执行云函数的结果:', res.result.data.length);
feedbackDataNum = res.result.data.length;
});
return feedbackDataNum
}
执行的结果是:
顺序反了。怎么同步执行或必须等待云函数返回结果后,执行自己的逻辑。
https://developers.weixin.qq.com/community/develop/article/doc/0006888c384020bc323db117851c13
返回promise,或者传入callback
学学async await