TypeError: Cannot read property ‘sendMessage’ of null
TypeError: Cannot read property ‘sendMessage’ of null
我正在尝试通过向它发送一个问候语来测试我的新 Slackbot,它根据我在 slackClient.js:
中的代码以 'this is a test message' 回复的预期行为
'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
console.log(message);
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel){
const rtm = new RtmClient(bot_token);
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;
这是我得到的错误:
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
^
TypeError: Cannot read property 'sendMessage' of null
如果我理解正确,它告诉我我不能给 rtm 一个 null 值,但是我可以给它什么样的样板值以便我可以测试它?
我发现了我的错误,我将 rtm 作为 const,然后是 let。所以我从 rtm = new RtmClient(bot_token); 中删除了 const然后像这样继续添加 nlp:
'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
let nlp = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel, nlpClient){
rtm = new RtmClient(bot_token);
nlp = nlpClient;
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;
我正在尝试通过向它发送一个问候语来测试我的新 Slackbot,它根据我在 slackClient.js:
中的代码以 'this is a test message' 回复的预期行为'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
console.log(message);
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel){
const rtm = new RtmClient(bot_token);
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;
这是我得到的错误:
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
^
TypeError: Cannot read property 'sendMessage' of null
如果我理解正确,它告诉我我不能给 rtm 一个 null 值,但是我可以给它什么样的样板值以便我可以测试它?
我发现了我的错误,我将 rtm 作为 const,然后是 let。所以我从 rtm = new RtmClient(bot_token); 中删除了 const然后像这样继续添加 nlp:
'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
let nlp = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel, nlpClient){
rtm = new RtmClient(bot_token);
nlp = nlpClient;
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;