各位大佬,本人初学者,在学习编写一个投票小程序,在投票页面调用云函数时分为匿名和实名,匿名调用正常,但是在实名的时候就报错:Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: Error: docId必须为字符串或数字,发现实名投票调用云函数时候Postdata中的VoteID传过去了,但是经过console.log(res.result)打印却为空,请大佬指点为何?
云函数:
const cloud = require('wx-server-sdk')
cloud.init()
const db=cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid=wxContext.OPENID
const {pickedOption,userInfo,voteID}=event.postData
const voteItem=userInfo?{
openid,
pickedOption,
userInfo
}:{
openid,
pickedOption
}
return await db.collection('vote').doc(voteID).update({
data:{
voteList:db.command.push(voteItem)
}
})
}
本地调用:
onTapVote(){
if (this.data.isAnonymous){
const postData={
voteID:this.data.voteID,
pickedOption:this.data.pickedOption
}
wx.cloud.callFunction({
name:'vote',
data:{
postData
}
}).then(res=>{
console.log(res.result)
this.getVoteStatusFromServer(this.data.VoteID)
}).catch(res=>{
console.error(res)
wx.showToast({
title: '匿名投票失败',
icon:'none'
})
})
}else {
const _this=this
wx.getUserProfile({
desc: '正在获取',
success(res){
const postData = {
VoteID:_this.data.voteID,
userInfo:res.userInfo,
pickedOption:_this.data.pickedOption
}
wx.cloud.callFunction({
name:'vote',
data:{
postData
}
}).then(res=>{
console.log(res.result)
}).catch(res=>{
console.error(res)
wx.showToast({
title: '实名投票失败',
icon:'none'
})
})
//_this.getVoteStatusFromServer(_this.data.VoteID)
},
fail(res){console.log(res)},
})
}
},
我也是在看《微信小程序开发从入门到实战》的小白,按照书上操作做投票小程序这个,碰见相同的问题,想问一下是哪里出错了?向你请教
云函数里面打印下voteID看看 就知道有没有传过去了