为什么我无法从 angular.js 工厂获取对象 firebase?

Why I can't get object firebase from angular.js factory?

我有工厂

    o.getUserWorkout = function (_id) {
        if (_id){
            $firebaseArray(DBC.getRef().child('workouts')
                    .orderByChild('userID')
                    .equalTo(DBC.getAuthRef().$getAuth().uid)
            ).$loaded()
                .then(function (x) {
                    console.log(x.$getRecord(_id))
                    return x.$getRecord(_id);
                });
        }
    };

和控制器

function workoutController($scope, workoutFct, $state) {
    $scope.editUserWorkout = function (_id) {
        console.log(workoutFct.getUserWorkout(_id));
    };
}

为什么我得到对象的正确值

console.log(x.$getRecord(_id))

在控制台中

Object {description: .......

但在控制器中

console.log(workoutFct.getUserWorkout(_id));

我明白了

undefined

您的服务返回承诺,因此您需要使用 .then 来访问返回的数据

workoutFct.getUserWorkout(_id).then(function(id){ 
    console.log(id);
});

更新

workoutFct.getUserWorkout(_id).$loaded().then(function (x) 
{
    console.log(x.$getRecord(_id)); 
});