云函数代码如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
// 云函数入口函数
exports.main = async (event, context) => {
try {
return await cloud.openapi.subscribeMessage.send({
"touser": event.openid, //要推送给那个用户
"templateId":event.templateId, //模板id
"page": event.page, //要跳转到那个小程序页面
"data": event.sendData,//要推送的消息内容
"miniprogramState": event.miniprogramState //小程序的版本
})
} catch (err) {
console.log('云函数发送信息失败')
console.log(err)
}
}
js调用云函数的片段代码如下:
// 下发通知信息
let sendData = {
"date1": {
"value": '2024年2月6日'
},
"time2": {
"value": '10:00'
},
"thing6": {
"value": 'XX课程'
},
"thing11": {
"value": '第1次课程'
},
"thing5": {
"value": '模拟测试'
}
}
wx.cloud.callFunction({
name: 'sendMessage',
data: {
openid: ‘接收信息的微信openid’,
templateId: ‘具体的模板id’,
page: '点击消息打开的页面',
sendData: sendData,
miniprogramState: 'developer',
},
success: res => {
console.log('消息发送成功')
console.log(res)
},
fail: error => {
console.log('消息发送失败')
console.log(error)
}
})
执行上面代码后,在手机微信上可以收到通知消息,但是console却输出错误信息,如下:
Error: cloud.callFunction:fail Error: errCode: -504002 functions execute fail | errMsg: TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at callback (/var/runtime/node16/CallbackContext.js:31:23)
at succeed (/var/runtime/node16/CallbackContext.js:57:7)
at processTicksAndRejections (node:internal/process/task_queues:96:5) (callId: 1707200890460-0.9296804752375667) (trace: 14:28:10 start->14:28:11 system error (Error: errCode: -504002 functions execute fail | errMsg: TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at callback (/var/runtime/node16/CallbackContext.js:31:23)
at succeed (/var/runtime/node16/CallbackContext.js:57:7)
at processTicksAndRejections (node:internal/process/task_queues:96:5)), abort)
at R (<anonymous>:1:245361)
at <anonymous>:1:279438
别直接return ,定义个变量,return变量
// 云函数入口函数
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.subscribeMessage.send({
openid: ‘接收信息的微信openid’,
templateId: ‘具体的模板id’,
page: '点击消息打开的页面',
sendData: sendData,
miniprogramState: 'develope
})
return result
} catch (err) {
console.log(err)
return err
}
}
改成 return ‘success’,不把result返回出去,能够顺利通过,不过想了解一下subscribeMessage.send返回的对象包括哪些内容,能否答复一下,谢谢!