-1

I am creating a MEAN stack webapp and I am having trouble with promises in the Node / Express side.

I have my router file, with a route set up like this:

router.get('/tweets', (req, res) => {
  let mc = new MeaningCloudModel();
  let tweets = "randomstring123";
  mc.getTopics(tweets)
  .then((topics)=> {
    res.send(topics);
  });
})

The MeaningCloudModel code is here:

let MeaningCloud = require('meaning-cloud');

class MeaningCloudModel{

  constructor() {
    this.meaning = MeaningCloud({
      //config defined here
      }
    });
  }

  getTopics(tweetStr){
      this.meaning.topics_extraction({
        lang: 'en',
        txt: tweetStr,
        tt: 'eco'
      }).then(function(res) {
        return (res.body);
      });
  }
}

module.exports = MeaningCloudModel;

When i navigate to that route I get a console error:

(node:8916) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'then' of undefined

I understand that I haven't set up my promises right, but I am not sure how to fix it.

The router calls the getTopics method on the MeaningCloudModel which itself is calling a method from the meaning-cloud dependency, which returns a promise. How do I return the output from that promise as a response to the '/tweets' route request?

Bhaumik Pandhi
  • 2,655
  • 2
  • 21
  • 38
joshua miller
  • 1,686
  • 1
  • 13
  • 22

1 Answers1

-2

It´s because you are trying to get the property "then" of an undefined object.

Try with this:

getTopics(tweetStr){
      return this.meaning.topics_extraction({
        lang: 'en',
        txt: tweetStr,
        tt: 'eco'
      }).then(function(res) {
        return (res.body);
      });
  }
tnovau
  • 23
  • 5
  • sorry, my fault, checking Promise documentation in [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). It seems like you have to return a new Promise. Checkout this. – tnovau Sep 07 '17 at 15:44