我编写了这个模块,用于通过HummingBird API搜索动画。模块加载良好,甚至帮助命令/help humming也返回适当的帮助消息。但我不能让它回应搜索查询
控制台只显示
An unhandled error occurred. Start as debug for details.
继续听下一条信息。我尝试过在调试模式下运行控制台(正如在Kassy中提到的那样),但是控制台显示了相同的消息。我试过用KPM安装模块,但结果是一样的。
这两个文件位于humming目录中的modules目录中。
humming.js
console.debug('line 10');
var request = require.safe('request');
/*
* Paths
*/
var HUMMINGBIRD_HOST = "hummingbird.me/api/v1";
var HUMMINGBIRD_SEARCH = "/search/anime/";
exports.match = function(text, commandPrefix) {
console.debug('line 23');
return text.startsWith(commandPrefix + 'humming');
};
/*
Method that provides help strings for use with this module.
*/
exports.help = function(commandPrefix) {
return [[commandPrefix + 'humming <query>','Searches Anime or Manga when you are too lazy to make a few clicks']];
};
/*
The main entry point of the module. This will be called by Kassy whenever the match function
above returns true.
*/
exports.run = function(api, event) {
var query = event.body.substr(8);
console.debug('line 40');
search(query, function(error, response){
// Callback calls the parser if no errors were registered
if(error !== null){
api.sendMessage(parse(response), event.thread_id);
} else{
console.debug(error);
}
});
};
function parse(query){
// testing
return JSON.stringify(query);
// return 'parser reached';
}
/**
* Retrieves information about an anime as a JSON object.
*
* query: string to search
* callback: takes the error, and data
*/
function search(query, callback) {
request.get({
url: "https://" + HUMMINGBIRD_HOST +
HUMMINGBIRD_SEARCH + "?query=" + query,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}, function(err, res, body) {
console.debug('line 73');
if(err) {
if(res) {
callback("Request error: " + err + ", " + res.statusCode, body);
}
else {
callback("Connection error: not connected to internet", body);
}
}
else {
callback(null, body);
}
});
}kassy.json
{
"name": "humming",
"version": 2.0,
"startup": "humming.js"
}您将注意到对调试器日志的一些调用,但这些调用从未显示在调试模式中,因此我不确定模块是否正在执行,或者我的调试是否有问题。这是我用来启用调试的启动命令。
node debug main.js facebook test
我还尝试将模块重命名为test、manga、anime等,前提是系统中可能存在歧义,但没有任何改变。
我对request.get方法的实现不太确定,因为我对Javascript很陌生,并且遵循了这个GitHub项目的方法语法
发布于 2016-02-11 23:28:34
我看得很快,无法复制未处理的错误,但是如果您将搜索回调中的代码块更改为
if(!error){
api.sendMessage(response, event.thread_id);
} else{
console.debug(error);
}
这将打印输出的原始响应。
如果再次发生异常,可以发送以下命令
/issue "title“描述全
这将将大量配置数据转储到K黑github上的一个问题上。
https://stackoverflow.com/questions/35328771
复制相似问题