javascript - 需要在 Slack App Node.js 的异步函数中获取 REST API
问题描述
我正在尝试为 Slack 创建一个在调用命令时调用外部 api 的应用程序。我做了以下事情。但问题是 Slack 框架使用了一个await
app.command("/command", async({ ack, payload, context })=>{
ack();
var details;
try{
apiHandler(function(details) {
const result = await app.client.chat.postMessage({ //<--- error here
token: context.botToken,
channel: payload.channel_id,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: details
},
}
],
text: "Message from Test App"
});
console.log(details);
});
}catch(error)
{
console.error(error);
}
});
//helper function:
function apiHandler(callback) {
let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let xhr = new XMLHttpRequest;
xhr.open("GET","API-URL",true);
xhr.onload = function()
{
if(this.status===200){
callback(JSON.parse(this.responseText););
}
}
xhr.send();
}
当我启动节点时,我得到以下信息:
SyntaxError: await 仅在异步函数中有效
我对 node.js 很陌生,我完全不知道该怎么做
解决方案
将 async 关键字放在apiHandler(async function(details) {
推荐阅读
- pdf-generation - 如何使用 getPDF 在 FIORI Web ide 中使用 oData Api A_BillingDocument 生成 PDF 文件
- regex - svn2git 正则表达式变量替换
- python - Selenium - 如何比较 get_attribute 值返回 None [Python]
- python-3.x - 如何从 Jupyter 上的计算机加载文件夹(带有文本文件)以便能够一起对它们进行分析?
- electron - 在电子应用程序中使用 axios 发出请求时的 HPE_INVALID_HEADER_TOKEN
- angular - 通过 TFS docker 部署在运行时设置 Angular 应用程序版本变量
- postgresql - DELETE FROM ... USING 在引用表为空时不删除任何行
- javascript - 未经检查的 runtime.lastError:消息端口在收到响应之前关闭
- excel - 仅打开 Internet Explorer 站点的 Shell 命令生成错误:参数无效或找不到文件
- c# - 端口 C 结构到 C#