订阅消息在云函数 中调用不成功?
发布于 6 年前 作者 guiying93 10690 次浏览 来自 问答

报的错误:WAService.js:1 Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 3f16ecb1-7012-11ea-88b1-5254003e0a60, cloud function service error code -504002, error message wx is not defined; 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

用的是wx-js-utils包

云函数:

const cloud = require('wx-server-sdk')

const {

WXMINIUser,

WXMINIMessage

} = require('wx-js-utils');


const appId = '******************'; // 小程序 appId

const secret = '**********************'; // 小程序 secret

const template_id = 'lPaxrYxe7OAKbkSXcn7aX8o0RFQSYNVeObOm1JrK11w'; // 小程序模板消息模板 id

cloud.init()

const db = wx.cloud.database();

const todos = db.collection("todos");


exports.main = async(event, context) => {

const wxContext = cloud.getWXContext()

let wXMINIUser = new WXMINIUser({

appId,

secret

});


let access_token = await wXMINIUser.getAccessToken();


const touser = wxContext.OPENID; // 小程序用户 openId,从用户端传过来,指明发送消息的用户

const form_id = event.form_id; // 小程序表单的 form_id,或者是小程序微信支付的 prepay_id

let task = await todos.doc(event.taskId).get()


// 发送模板消息

let wXMINIMessage = new WXMINIMessage();

let result = await wXMINIMessage.sendMessage({

access_token,

touser,

form_id,

template_id,

data: {

thing1: {

value: task.result.data.title

},

character_string5: {

value: task.data._id

},

time3: {

value: task.data.title

},

thing4: {

value: task.data.location.name

}

},

page: `pages/todosInfo/todosInfo?id=${task.data._id}` // 点击模板消息后,跳转的页面

});

return result;

}

1 回复

const db = cloud.database(); wx.去掉,云函数里没有这个

回到顶部