我正在使用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