【发布时间】:2016-11-03 20:51:05
【问题描述】:
我正在使用 restler (https://github.com/danwrong/restler) 从外部来源进行 api 调用。在 Sailsjs 中,据我了解,辅助函数称为服务。我将用于获取、发布等的 restler 代码放在他们自己的服务中,这样我就不会一遍又一遍地使用相同的代码重复自己。但是,在我的控制器中运行良好的 restler 函数不再在服务中运行。例如:
//api/services/myService.js
module.export{
httpGet: function(){
var rest = require('restler');
rest.get('http://google.com').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
this.retry(5000); // try again after 5 sec
} else {
console.log(result);
}
});
}
}
我知道我的服务被正确使用;我尝试从服务中返回一个变量以进行仔细检查:
httpGet: function(){
var check = null;
var rest = require('restler');
rest.get('http://google.com').on('complete', function(result) {
if (result instanceof Error) {
check = false;
console.log('Error:', result.message);
this.retry(5000); // try again after 5 sec
} else {
console.log(result);
check = true;
}
});
return check;
//in the controller, myService.httpGet() returns null, not true or false
}
任何帮助将不胜感激。 Salisjs v0.12.4
【问题讨论】: