问题描述
我只想为我的项目创建简单的 Slack 记录器。我的代码在这里;
import { SLACK_CHANNELS } from '@utility/constants/request';
const https = require('https');
export const sendSlackMessage = (messageBody,channel = SLACK_CHANNELS.SAMPLE_CHANNEL,title = 'Error Notifier',icon_emoji = ':bangbang:') => {
messageBody.icon_emoji = icon_emoji;
messageBody.title = title;
messageBody.channel = channel;
try {
messageBody = JSON.stringify(messageBody);
} catch (e) {
throw new Error('Failed to stringify messageBody',e);
}
const webhookURL = 'webhook_url';
// Promisify the https.request
return new Promise((resolve,reject) => {
// general request options,we defined that it's a POST request and content is JSON
const requestOptions = {
method: 'POST',header: {
'Content-Type': 'application/json',},};
// actual request
const req = https.request(webhookURL,requestOptions,(res) => {
let response = '';
res.on('data',(d) => {
response += d;
});
// response finished,resolve the promise with data
res.on('end',() => {
resolve(response);
});
});
// there was an error,reject the promise
req.on('error',(e) => {
reject(e);
});
req.write(messageBody);
req.end();
});
}
我收到了这个错误; 未处理的拒绝(类型错误):“侦听器”参数必须是函数类型。接收类型对象
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)