我目前正在尝试编写一个角度服务来处理第三方API。 API对象处理所有事情。您只需创建对象,在其上调用函数,然后获取有效负载。当我在调试器中单步调试服务函数时,我可以在findRole函数中看到JSON响应,但是它出错了:
TypeError: Cannot read property 'then' of undefined
有关如何使这项工作的任何想法?
var angular = require('angular'),
govtracksvc = require('./rep.service'),
Rep = require('./Rep'),
govtrack = require('govtrack-node');
angular.module('RepresentativeApp', [])
.value('govtrack', govtrack)
.factory('govtracksvc', govtracksvc)
.controller('Rep', Rep);
module.exports = angular.module('RepresentativeApp');
module.exports = function Rep (govtracksvc) {
var rep = this;
rep.list = govtracksvc.findRole(rep.govtrack);
};
module.exports = function govtracksvc(govtrack) {
var rep = this;
var data = {};
govtrack.findRole({current: true}, function (err, res) {
if (err) {
console.log('err: ', err);
}
return res;
}).then(function (res) {
data = res.objects;
});
return data;
};
如果有人想看到数据,那么这是端点:https://www.govtrack.us/api/v2/role/?current=true
答案 0 :(得分:0)
以下是使用回调的代码示例,看看它是否符合您的需要:
的模块强>
var angular = require('angular'),
govtracksvc = require('./rep.service'),
Rep = require('./Rep'),
govtrack = require('govtrack-node');
angular.module('RepresentativeApp', [])
.value('govtrack', govtrack)
.factory('govtracksvc', govtracksvc)
.controller('Rep', Rep);
module.exports = angular.module('RepresentativeApp');
<强>控制器强>
module.exports = function Rep (govtracksvc, $scope) {
var rep = this;
govtracksvc.findRole(rep.govtrack, function(result){
rep.list = result;
if(!$scope.$$phase){
$scope.$apply();
}
});
};
<强>服务强>
module.exports = function govtracksvc(govtrack, callback) {
var rep = this;
govtrack.findRole({current: true}, function (err, res) {
if (err) {
console.log('err: ', err);
return {};
}
return res.objects;
});
};