我正在尝试使用slack web api向用户发送直接消息,但我认为获取所有可用用户的getSlackUser 方法在我调用 slackId 时没有及时完成;console.log(slackId) 给出了未定义的意思,它没有用bolt完成我的 api 调用如何确保 getSlackUser 方法在继续执行其他方法之前完成(使其阻塞)?const { WebClient } = require('@slack/web-api');const { App } = require('@slack/bolt')const rtm = new RTMClient(process.env.SLACK_OAUTH_TOKEN);const web = new WebClient(process.env.SLACK_OAUTH_TOKEN);const app = new App({ token: process.env.SLACK_OAUTH_TOKEN, signingSecret: process.env.SLACK_SIGNING_SECRET});exports.handler = async (event) => { const slackId = await getSlackUser('example_real_name').id; console.log(slackId); await sendSlackMessage(slackId, 'Bot message');}sendSlackMessage = async (channel, message) => { await web.chat.postMessage({ channel: channel, text: message, as_user: true });}getSlackUser = async(real_name) => { const result = await app.client.users.list({ token: process.env.SLACK_OAUTH_TOKEN }); console.log(result); return result.members.find((user) => user.real_name == real_name);}
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
问题是这一行的优先级:
const slackId = await getSlackUser('example_real_name').id;
由于成员访问比 await 具有更高的优先级(之前已评估),因此它实际上与以下内容相同:
const slackId = await (getSlackUser('example_real_name').id);
getSlackUser
返回一个 Promise 对象,那么它的id
成员是未定义的。Await 等待未定义的,即undefined
.
要解决此问题,请确保在以下操作之前评估 await .id
:
const slackId = (await getSlackUser('example_real_name')).id;
添加回答
举报
0/150
提交
取消