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?