使用Node和Angular接收Resource对象而不是value

时间:2016-06-29 18:50:12

标签: angularjs node.js nested angular-promise

我试图通过授权getCurrentUser函数获取返回的值。在调用函数时,我得到了这个持有Promise的资源。我可以在那里看到_id值!我该如何访问它?

提前谢谢! Console Output

这里有一些Auth服务,其中定义了getCurrentUser:

(function config() {
  function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
    const safeCb = Util.safeCb;
    let currentUser = {};
    let Auth = {};
    const LOCAL_HOST = 'localhost';

    if ($cookies.get('token') && $location.path() !== '/logout') {
      currentUser = User.get();
    }

    Auth = {
      getCurrentUser(callback) {
      if (arguments.length === 0) {
        return currentUser;
      }

      const value = currentUser.hasOwnProperty('$promise') ? currentUser.$promise : currentUser;
      return $q.when(value).then(user => {
        safeCb(callback)(user);
        return user;
      }, () => {
        safeCb(callback)({});
        return {};
      });
    }
...

这就是我所说的:

    angular.module('toroApp').directive('csvreaderDirective', ['$http', function ($http, Auth) {

      return {
        controller(Auth) {
          const currentUser = Auth.getCurrentUser();
          console.log(currentUser);
        },
        compile(element, Auth) {
    ...

1 个答案:

答案 0 :(得分:0)

Auth.getCurrentUser()返回$q promise,因此您需要使用promise API来获取结果。

controller(Auth) {
    Auth.getCurrentUser().then(function (user) {
        const currentUser = user;
        console.log(currentUser);
    });
}