I'm having some issues getting a custom promise to work with my little app I'm working on.
Here is an abbreviated version of my app.js
var Promise = require('promise');
var Commands = require('./commands');
var respond = function() {
var messageToRespondWith = command[0].response;
if(typeof(command[0].response) == "function"){
console.log('is function');
command[0].response().then(console.log('finished'));
}
var response = template(messageToRespondWith, {user: user, channel: channel, message: message});
client.say(channel, response);
};
And the commands look something like this
var config = require('./config');
var Promise = require('promise');
module.exports = [
{
trigger: "!random",
response: function(){
var unirest = require('unirest');
var promise = new Promise(function(resolve, reject) {
unirest.post("https://andruxnet-random-famous-quotes.p.mashape.com/cat=movies")
.header("X-Mashape-Key", config.mashapeKey)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.end(function (result) {
console.log(result.status);
// Handle errors
if(result.status != 200){
return promise.reject(result.body);
}else{
var json = JSON.parse(result.body);
return promise.resolve(json.quote);
}
});
});
},
permission: null
},
]
The app.js
code checks to see if the returned value type is a function, and if it is it executes it and is supposed to save that to a variable and pass that along to the next function.
I need to use a promise because obviously the bottom function call client.say()
will not wait for the returned value form the API call of the function from the commands.js
file.
When I try to execute this code, in my terminal I get this error...
TypeError: Cannot read property 'then' of undefined
at respond (/***/app.js:39:28)
So I'm not really sure why this is happening, I'm still learning node and the best ways to do things so maybe I'm on the wrong track here altogether.
Basic functionality
This is a twitch IRC chat bot that will read commands being typed in and respond in chat with a predetermined message. In some cases I will want to do some sort of function before spitting out the message. This is one of those cases. You can see my full app.js
code here I didn't want to paste the entire thing because it's long and include parts that I thought were not relevant here.