async function getMessageName(tid) {
message.where({
templateid: tid
}).get().then(res => {
console.log(res.data)
if (res.data) {
let sendData = []
for (let i in res.data) {
let oneData = {
touser: res.data[i].subscriber,
templateId: tid,
miniprogram_state: 'developer',
page: 'pages/index/index',
// 此处字段应修改为所申请模板所要求的字段
data: res.data[i].messageItem
}
sendData.push(oneData)
}
console.log('sendData:')
console.log(...sendData)
return sendData
// return "here is the return"
}
}).catch(err => {
console.log(err)
})
}
async function aaa(tid) {
return "here is the return"
}
async function sendSubscribeMessage(event) {
console.log("here i am")
let prepareData = await aaa(event.templateId)
console.log(prepareData) // 这句 是 "here is the return"
prepareData = await getMessageName(event.templateId)
console.log(prepareData) // 这句 是 undefined
console.log("here i am 2")
let sendResult = []
for (let i in prepareData) {
sendResult.push(await cloud.openapi.subscribeMessage.send(prepareData[i]))
console.log("here i am 3")
}
return sendResult
}
下面是云函数本地调试的 Console提示
[info] 函数被触发,正在执行中...
index.js:17 {action: "sendSubscribeMessage", templateId: "txPkadLVHO4lWYSPd_y01YH9go0P2FE8AHIdt6nMno4", datat: {…}}
index.js:91 here i am
index.js:93 here is the return
index.js:95 undefined
index.js:96 here i am 2
node.js:1 [info] 函数执行成功(耗时 15ms) []
node.js:1 [info] 调用 本地 云函数 'openapi' 完成 (耗时 19ms)
index.js:42 (2) [{…}, {…}]
index.js:58 sendData:
index.js:59 {touser: "ojFQH5ttjBG8QPnE-xgbvIj763xg", templateId: "txPkadLVHO4lWYSPd_y01YH9go0P2FE8AHIdt6nMno4", miniprogram_state: "developer", page: "pages/index/index", data: {…}} {touser: "ojFQH5uc10PCwpS48EzU-jykyV94", templateId: "txPkadLVHO4lWYSPd_y01YH9go0P2FE8AHIdt6nMno4", miniprogram_state: "developer", page: "pages/index/index", data: {…}}
先了解下为什么要用async await