我使用Backand作为我的数据库,他们在他们的github上提供了一个todo应用程序,但它只检索用户的所有数据。我只想检索登录用户的数据。我怎么能这样做?
有点,我想在我的services.js上修改它:
service.all = function () {
return $http.get(getUrl());
};
我想传递当前用户的对象。
答案 0 :(得分:0)
有一个用户演示的待办事项。 https://github.com/backand/angular-yeoman-todos/tree/todo-with-users
最终,我的用户服务看起来像这样......
angular.module('app.services')
.service('userService', ['Backand', '$state', '$q', '$http', 'authService', function(Backand, $state, $q, $http, authService) {
this.getUserByEmail = function(email) {
var resultsDeferred = $q.defer();
var requestDeferred = $http({
method: 'GET',
url: Backand.getApiUrl() + '/1/objects/users',
params: {
pageSize: 1,
pageNumber: 1,
filter: [{
fieldName: 'email',
operator: 'equals',
value: email
}],
sort: ''
}
});
requestDeferred.then(function(response) {
var user = response.data.data;
resultsDeferred.resolve(user); // allow the controller to render
}, function failureCallback(reason) {
resultsDeferred.reject(reason); // bubble up failure
});
return resultsDeferred;
}
}]);
您收到来自...的电子邮件
var user = Backand.getUserDetails();
var userEmail = null;
if (user.$$state.value !== null) {
userEmail = user.$$state.value.username;
} else {
userEmail = null;
}
var userDeferred = userService.getUserByEmail($scope.userEmail).promise;
userDeferred.then(function successCallback(response) {
$scope.user = response[0];
console.log('userId (email): %o', $scope.user.id);
console.log('user phone: %o', $scope.user.phone);
});