1

TypeError: DialogflowHandle.handleMessage is not a function

DialogflowHandle.handleMessage(message.body) // here notify handle not a funtion.

And here is class:

const dialogflow = require('dialogflow');
class DialogflowHandle {

  handleMessage (sentence) {
    this.request.queryInput.text.text = sentence
    return new Promise(
      (resolve, reject) => {
        this.sessionClient.detectIntent(this.request)
          .then(resolve)
          .catch(reject);
      }
    )
  }
}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • 1
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Aug 29 '19 at 08:09

2 Answers2

2

handleMessage is a method available on instances of DialogflowHandle. It isn't available as a method of the DialogflowHandle constructor function.

const dfh = new DialogflowHandle();
dfh.handleMessage(message.body);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if I use : const dfh = new DialogflowHandle(); dfh.handleMessage(message.body) Error is :TypeError: DialogflowHandle is not a constructor – Nguyễn Nhi Phát Aug 29 '19 at 07:55
  • @NguyễnNhiPhát — I can't reproduce that problem: https://jsbin.com/sidepeyegi/1/edit?js,console It must have something to do with code you haven't shared with us. – Quentin Aug 29 '19 at 08:21
0

You should add static keyword to your handleMessage method in order you want it to be accessed without creating DialogflowHandle object:

const dialogflow = require('dialogflow');
class DialogflowHandle {

  static handleMessage (sentence) {
    this.request.queryInput.text.text = sentence
    return new Promise(
      (resolve, reject) => {
        this.sessionClient.detectIntent(this.request)
          .then(resolve)
          .catch(reject);
      }
    )
  }
}
kaxi1993
  • 4,535
  • 4
  • 29
  • 47