Javascript 如何在新AngularFire v0.8.0中实现$child方法?

Javascript 如何在新AngularFire v0.8.0中实现$child方法?,javascript,angularjs,angularfire,Javascript,Angularjs,Angularfire,用户登录到网站并尝试创建帖子。每当创建新帖子时,此帖子都会与创建帖子的用户关联 指的是使用AngularFire的旧API的 当使用AngularFire API v0.8.0时,添加post中断的这行代码: user.$child('posts').$child(postId).$set(postId); 具有创建Post方法的Post工厂(Post.js)是: app.factory('Post', function ($firebase, FIREBASE_URL, User) {

用户登录到网站并尝试创建帖子。每当创建新帖子时,此帖子都会与创建帖子的用户关联

指的是使用AngularFire的旧API的

当使用AngularFire API v0.8.0时,添加post中断的这行代码:

user.$child('posts').$child(postId).$set(postId);
具有创建Post方法的Post工厂(Post.js)是:

app.factory('Post',
function ($firebase, FIREBASE_URL, User) {
  var ref = new Firebase(FIREBASE_URL + 'posts');

  var posts = $firebase(ref).$asArray();

  var Post = {
    all: posts,
                                       //Starting of create function
    create: function (post) {
      if (User.signedIn()) {
        var user = User.getCurrent();   //Gets the current logged in user
        post.owner = user.username;

        return posts.$add(post).then(function (ref) {
          var postId = ref.name();

          user.$child('posts').$child(postId).$set(postId); 

          //user.$getRecord('posts').$getRecord(postId).$set(postId);             

          return postId;
        });
      }
    },
                                       //End of create function
因为AngularFire说

$child()
不再存在。父对象中已存在数据,因此不鼓励创建其他同步子对象。使用数据转换、展平数据,或下拉到Firebase SDK并使用其
child()
方法

我对如何更改代码以使用API中的更新感到困惑

编辑后

这是getCurrent方法:

getCurrent: function(){ // retrieves current user
  return $rootScope.currentUser;
},
属于user.js工厂的

'use strict';

app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
 var ref = new Firebase(FIREBASE_URL + 'users');

 var users = $firebase(ref);
 var usersdiv = $firebase(ref).$asArray();

 var User = {

    create: function (authUser, username) {
      users[username] = {
        md5_hash: authUser.md5_hash,
        username: username
      };
      users.$update(username, {
          md5_hash: authUser.md5_hash,
          username: username
      }).then(function (dataRef) {
          dataRef.setPriority(authUser.uid);
          setCurrentUser(username);
      });
    }, // end of create method

    findByUsername: function(username){
      if(username){
        return usersdiv.$getRecord(username);
      }
    },

    getCurrent: function(){ // retrieves current user
      return $rootScope.currentUser;
    },

    signedIn: function(){ //checks if user is signed in
      return $rootScope.currentUser !== undefined;
    }

  }; // end of User

  // so that we can pull info about user when logged in
  function setCurrentUser (username){
    $rootScope.currentUser = User.findByUsername(username);
  }

  //for logins and refreshes
  $rootScope.$on('$firebaseSimpleLogin:login', function(e, authUser){

      var queryRef = ref.startAt(authUser.uid).endAt(authUser.uid);

      var queryArray = $firebase(queryRef).$asArray();

      queryArray.$loaded().then(function() {
        setCurrentUser(queryArray.$keyAt(0));
      });
  });

  //logout
  $rootScope.$on('$firebaseSimpleLogin:logout', function(){
    delete $rootScope.currentUser;
  });
  return User;
});

只需在Firebase中设置一个值,就不需要在本地创建同步对象($child过去所做的)。您可以在任何时候使用已创建的Firebase ref执行此操作。我不能确切地说出用户的数据结构是什么,因为它没有包括在内,但类似于这样:

new Firebase(FIREBASE_URL).child('...path/to/posts').child(postId).set(postId);

很可能,这属于您的用户对象,因此在Post factory中,您只需执行类似于
user.addPost(postId)

的操作,就不需要在本地创建同步对象(就像$child过去所做的那样)即可在Firebase中设置值。您可以在任何时候使用已创建的Firebase ref执行此操作。我不能确切地说出用户的数据结构是什么,因为它没有包括在内,但类似于这样:

new Firebase(FIREBASE_URL).child('...path/to/posts').child(postId).set(postId);

最有可能的情况是,这属于您的用户对象,因此在Post factory中,您可以执行类似于
user.addPost(postId)
的操作。我也面临同样的问题。正如Kato所建议的,您必须在Firebase对象中使用子函数。我选择将帖子添加到帖子工厂本身的用户

向用户添加帖子 整个post.js如下所示:

“严格使用”;
附件工厂(‘邮政’,
函数($firebase、firebase\u URL、用户){
var ref=新的Firebase(Firebase_URL+‘posts’);
var usersref=新Firebase(Firebase_URL+“用户”);
var posts=$firebase(ref)。$asArray();
var Post={
所有:员额,
创建:功能(post){
if(User.signedIn()){
var user=user.getCurrent();
post.owner=user.username;
返回post.$add(post).然后(函数(ref){
var postId=参考名称();
usersref.child(post.owner).child('posts').child(postId).set(postId);
回帖;
});
}
},
查找:功能(postId){
返回$firebase(ref.child(postId)).$asObject();
},
删除:功能(postId){
if(User.signedIn()){
var postToDel=Post.find(postId);
postToDel.$loaded().then(函数()){
var p=职位[postToDel.$id];
posts.$remove(posted).then(function(){
$firebase(usersref.child(p.owner.child('posts')).$asArray().$remove(p.$id);
});
});
}
}
};
回程站;
});

我也面临同样的问题。正如Kato所建议的,您必须在Firebase对象中使用子函数。我选择将帖子添加到帖子工厂本身的用户

向用户添加帖子 整个post.js如下所示:

“严格使用”;
附件工厂(‘邮政’,
函数($firebase、firebase\u URL、用户){
var ref=新的Firebase(Firebase_URL+‘posts’);
var usersref=新Firebase(Firebase_URL+“用户”);
var posts=$firebase(ref)。$asArray();
var Post={
所有:员额,
创建:功能(post){
if(User.signedIn()){
var user=user.getCurrent();
post.owner=user.username;
返回post.$add(post).然后(函数(ref){
var postId=参考名称();
usersref.child(post.owner).child('posts').child(postId).set(postId);
回帖;
});
}
},
查找:功能(postId){
返回$firebase(ref.child(postId)).$asObject();
},
删除:功能(postId){
if(User.signedIn()){
var postToDel=Post.find(postId);
postToDel.$loaded().then(函数()){
var p=职位[postToDel.$id];
posts.$remove(posted).then(function(){
$firebase(usersref.child(p.owner.child('posts')).$asArray().$remove(p.$id);
});
});
}
}
};
回程站;
});
正确答案是:

'use strict';

app.factory('Post',
    function ($firebase, FIREBASE_URL, User) {
      var postsref = new Firebase(FIREBASE_URL + 'posts');
      var usersref = new Firebase(FIREBASE_URL + 'users');   
      var posts = $firebase(postsref).$asArray();     

      var Post = {
        all: posts,

        create: function (post) {
          if (User.signedIn()) {
            var user = User.getCurrent();        
            post.owner = user.username;      
            return posts.$add(post).then(function (ref) {
              var postId = ref.name();
              //a child in user forge should be made with its key as postID
              usersref.child(post.owner).child('posts').child(postId).set(postId);
              return postId;
            });
          }
        },

        find: function (postId) {
          return $firebase(postsref.child(postId)).$asObject();
        },

        delete: function (postId) {
          if (User.signedIn()) {
            var postToDel = Post.find(postId);
            postToDel.$loaded().then(function(){
                var p = posts[postToDel.$id];
                posts.$remove(postId).then(function(){
                    $firebase(usersref.child(p.owner).child('posts')).$remove(p.$id);                    
                });
            });
          }
        },
因此,可以在Firebase SDK级别上使用child。

例如:

正确答案是:

'use strict';

app.factory('Post',
    function ($firebase, FIREBASE_URL, User) {
      var postsref = new Firebase(FIREBASE_URL + 'posts');
      var usersref = new Firebase(FIREBASE_URL + 'users');   
      var posts = $firebase(postsref).$asArray();     

      var Post = {
        all: posts,

        create: function (post) {
          if (User.signedIn()) {
            var user = User.getCurrent();        
            post.owner = user.username;      
            return posts.$add(post).then(function (ref) {
              var postId = ref.name();
              //a child in user forge should be made with its key as postID
              usersref.child(post.owner).child('posts').child(postId).set(postId);
              return postId;
            });
          }
        },

        find: function (postId) {
          return $firebase(postsref.child(postId)).$asObject();
        },

        delete: function (postId) {
          if (User.signedIn()) {
            var postToDel = Post.find(postId);
            postToDel.$loaded().then(function(){
                var p = posts[postToDel.$id];
                posts.$remove(postId).then(function(){
                    $firebase(usersref.child(p.owner).child('posts')).$remove(p.$id);                    
                });
            });
          }
        },
因此,可以在Firebase SDK级别上使用child。

例如:


欢迎来到StackOverflow!您应该包含User.getCurrent()方法并返回,因为这是您失败的关键一行。人们不应该为了帮助你回答这里的问题而去阅读教程。很抱歉,你必须通读一遍。我原以为getCurrent()工作得很好,所以我没有把它包括在问题中,这是我的错误。我已经添加了用户工厂。和getCurrent()方法供您参考。欢迎使用StackOverflow!你应该