Thursday 4 May 2017

Calling a function that is defined within the same module.exports block

I'm new to node.js so apologies if this is obvious. I have a similar problem to this person, who wanted to be able to call a function either from within or from outside the same file in which it is defined. I also want that. But in my case both the function that I want to call, and the code in which I want to call it, are both already inside of a "module.exports" block.

Here is the relevant part of my code:

module.exports = class BotInstance extends EventEmitter {

  onGatewayMessage(message) {
    sendMessage(this.botID, msg, (sendStatus) => {
      console.log(`message successfully sent with status ${sendStatus}`);
    });
  }

  sendMessage(message, cb) {
    const msg = {
      message_id: uuidV1(),
      text: {
        content: message,
        mention: [],
        link_preview: [],
      },
    };
    this.service.sendMessage(this.botID, msg, cb);
  }

}

The suggestions given in the earlier question don't work in my case. How can I call the "sendMessage" function in my "onGatewayMessage" routine?



via Jeremy Malcolm

No comments:

Post a Comment