在云开发中想换题库集合,于是 把问题集合question改为 question_s_j。在云函数 questionPool中,把云函数selectRecord改为selectRecord_s_j,将getPageData 改 getPageData_s_j , getRandomList改 getRandomList_s_j,但更新云函数就出错(await is only valid in async)哦。怎么回事呢?
应该怎么改呢?请指点。
在selectRecord 的index.js
const cloud = require('wx-server-sdk');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
/**
* 分页查询题目列表
* @param {object} event
*/
async function getPageData(event){
const queryResult = await db.collection('question')
.skip((event.page - 1) * event.size)
.limit(event.size)
.get();
const {data, errMsg} = queryResult;
if (errMsg == "collection.get:ok"){
return {
errCode:0,
errMsg:errMsg,
questionList:data,
}
}else{
return {
errCode:1,
errMsg:errMsg,
}
}
}
/**
* 随机查询题目列表
* @param {object} event
*/
async function getRandomList(event){
const queryResult = await db.collection('question')
.aggregate()
.sample({
size: event.size
})
.end()
console.log(queryResult);
const {list, errMsg} = queryResult;
if (errMsg == "collection.aggregate:ok"){
return {
errCode:0,
errMsg:errMsg,
questionList:list,
}
}else{
return {
errCode:1,
errMsg:errMsg,
}
}
}
// 查询数据库集合云函数入口函数
exports.main = async (event, context) => {
// 返回数据库查询结果
// return getPageData(event)
return getRandomList(event);
};
// 查询数据库集合云函数入口函数
//exports.main = async (event, context) => {
// 返回数据库查询结果
return await db.collection('question')
.skip((event.page - 1) * event.size)
.limit(event.size)
.get();
//};
前端 改成这样
data: {
currentIndex: 0,
total: 0,
question: null,
questionList: [],
finish:false,
score:0,
correctCount:0,
wrongCount:0,
},
getList() {
const that = this;
wx.cloud
.callFunction({
name: "questionPool",
data: {
type: "selectRecord_s_j",
// page: 1,
size:30,
},
})
.then((res) => {
// console.log(res.result)
const { questionList_s_j, errMsg, errCode } = res.result;
if (errCode == 0) {
const total = questionList.length;
const question = questionList[that.data.currentIndex];
that.checkStar(question._id);
that.setData({
questionList_s_j,
total,
question_s_j,
});
} else {
console.error(errMsg);
wx.showToast({
title: "查询题目失败",
icon: "error",
});
}
})
.catch(console.error);
},