我正在使用restler(https://github.com/danwrong/restler)从外部来源进行api调用。在Sailsjs中,根据我的理解,帮助函数被称为服务。我把rest,rest等代码放在自己的服务中,这样我就不会重复使用相同的代码。但是,在我的控制器中正常工作的restler函数不再适用于该服务。例如:Sailsjs外部模块无法使用
//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
一件小事。我认为应该有'''module.exports = {'''。否则会出现语法错误。 – Bonanza
@Bonanza是的。确实。这是一个错字。 :) – MjZac
谢谢。我从根本上误解了回调;我试图在函数中返回一个值。 – hamncheez