写了一个云函数,调用后出现错误 :
WAService.js:1 Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID local_debug_b580f4c9-7026-40a5-a81b-ecf4814829b2, cloud function service error code -1, error message errCode: -501007 invalid parameters | errMsg: [InvalidParameter] Check request parameter fail. Please check your request, but if the problem cannot be solved, contact us.; ; at cloud.callFunction api;
at new u (WAService.js:1)
at d (WAService.js:1)
at f (WAService.js:1)
at Function.success (WAService.js:1)
at WAService.js:1
at C (WAService.js:1)
at i.<anonymous> (WAService.js:1)
at i.emit (WAService.js:1)
at Rs (WAService.js:1)
at WAService.js:1
云函数如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const command = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
if(!wxContext.OPENID){
return {status: 'fail'}
}
let today = new Date().toLocaleDateString()
let result;
//限流
await db.collection('t_map_request').where({
userId: wxContext.OPENID,
reqTime: command.and(command.gte(new Date(today+' 00:00:00')),command.lte(new Date(today+' 23:59:59')))
}).get().then(res => {
if(res.data.length>10000){
result = {event, status: 'limit'}
}
if(res.data.length>2000){
let runCount = 0;
res.data.forEach(element => {
if(element.bestPoi==null){
runCount++;
}
});
if(runCount>2){
result = {event, status: 'wait'}
}
}
})
if(result!=null){
return result;
}
//数据准备
let points = event.points //数组
let modes = event.modes //数组
let time = event.time
let type = event.type
let reqTime = new Date()
//数据入库
await db.collection('t_map_request').add({
data: {
userid: wxContext.OPENID,
locations: points,
modes: modes,
startTime: new Date(time+':00'),
reqTime: reqTime
}, success: res => {
result = {event, status: 'success', id: res._id}
}, fail: err => {
result = {event, status: 'fail'}
}
});
if(result==null){
result = {event, status: 'success'}
}
return result;
}
调用:console.log(JSON.stringify(res));返回也为{}
commitForm: function(points,modes,startDate,type,openId){
wx.showLoading('提交中...');
let pointsStr = JSON.stringify(points);
let modeStr = JSON.stringify(modes);
console.log('进入表单提交,receiveRequest | points='+pointsStr
+'&modes='+modeStr
+'&time='+startDate
+'&type='+type
+'&userId='+openId);
try {
var res = wx.cloud.callFunction({
name: 'receiveRequest',
data: {
points: points,
modes: modes,
time: startDate,
type: type
}
});
console.log(JSON.stringify(res));
wx.hideLoading();
if (res.result.status == 'limit'){
console.log('结束表单提交:LIMIT');
app.showToast('抱歉!今日达到上限,请明日再尝试', 2000);
}else if(res.result.status == 'wait'){
console.log('结束表单提交:WAIT');
app.showToast('系统正在为您处理上一个请求,请稍后再提交', 2000);
}else{
console.log('结束表单提交:SUCCESS');
wx.cloud.callFunction({
name: 'handleRequest',
data: {
points: points,
modes: modes,
time: startDate,
type: type
}
});
}
return true;
}catch (err) {
return false;
}
}
你好,请检查代码中 callFunction 的位置是否正确传递参数。同时看到您的业务代码中,缺少了 await/then/回调等待 callFunction 异步结果的步骤,这样 res 是一个 Promise 而不是具体的返回结果。可以再自查一下。
Check request parameter fail.
请求参数错误