【问题标题】:TypeError: Cannot read property ‘sendMessage’ of nullTypeError:无法读取 null 的属性“sendMessage”
【发布时间】:2017-10-14 19:55:45
【问题描述】:

我正在尝试根据我在 slackClient.js 中的代码向我的新 Slackbot 发送带有预期行为的问候语来测试我的新 Slackbot:

'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 值,但是我可以给它什么样的样板值以便我可以测试呢?

【问题讨论】:

    标签: node.js bots slack-api slack


    【解决方案1】:

    我发现了我的错误,我将 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;
    

    【讨论】:

    • 很抱歉问你,我知道为时已晚,但你能告诉我 nlpclient 是什么吗?
    • @RahulDudharejiya,用于访问自然语言 API。
    猜你喜欢
    • 2020-01-29
    • 2022-01-12
    • 2021-12-04
    • 2021-12-17
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多