Firebase + Angular +用户关联项

时间:2015-01-16 06:14:57

标签: javascript angularjs firebase angularfire

如果您查看下面的代码,我可以添加一个项目并将其与用户关联,但是当我想要启动用户项目时,我一直收到错误:

TypeError: undefined is not a function

我也试过在顶部使用$ child,但这也没有用。这是我的代码:

'use strict';
angular.module('writer')

  .factory('Users', ['firebaseURI', '$firebase', 'Auth', function(firebaseURI, $firebase, Auth){

    var currentUser = Auth.currentUser,
        users = new Firebase(firebaseURI + 'users');

    function _getProjectsForCurrentUser(){
      return users.child(currentUser.id + '/projects/');
    }

    function _addProjectForCurrentUser(projectRef){
      users.child(currentUser.id + '/projects/' + projectRef.key()).set(true);
    }

    return {
      getProjectsForCurrentUser: _getProjectsForCurrentUser,
      addProjectForCurrentUser: _addProjectForCurrentUser
    };

  }])

  .factory('Projects', ['firebaseURI', '$firebase', 'Auth', 'Users', function(firebaseURI, $firebase, Auth, Users){

    var projectsRef = new Firebase(firebaseURI + 'projects'),
        projects = $firebase(projectsRef);


    function _getProjects(){
      return projects;
    }

    function _create(project){
      console.log('create', project);
      projects.$push(project).then(function(ref){
        Users.addProjectForCurrentUser(ref);
      }, function(error){
        console.log('Projects Error:', error);
      });
    }

    return {
      getProjects: _getProjects,
      create: _create
    };

  }])
;

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

Users工厂的退货区块应为:

return {
  getProjectsForCurrentUser: _getProjectsForCurrentUser,
  addProjectForCurrentUser: _addProjectForCurrentUser
};

请注意额外的_