将Firebase对象移动/复制到另一个子节点 [英] Move/Copy Firebase Object to Another Child Node

查看:118
本文介绍了将Firebase对象移动/复制到另一个子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firebase中有一个用户对象,我需要移动到同一棵树上的另一个子节点。

I have a user object in firebase that I need to move to another child node on the same tree.

结构如下:

users -> male   -> uid -> ......KVPs..objects...etc
      -> female -> uid -> ......KVPs..objects...etc

我需要让用户分开由于应用程序的性质,性别。
如果用户更改了他们的性别,我想将他们的所有详细信息移动到所选择的性别。

I need to keep users separated by gender due to the nature of the app. If a user changes their gender I want to move all their details to that selected gender.

我尝试使用AngularFire(firebaseObject)但是当我使用它时会抛出错误由于对象中存在键,因此尝试将其设置在firebase中。我尝试使用JSON.stringify,angular.toJSON去掉键,但是没有运气!!

I tried using AngularFire (firebaseObject) but it throws errors when I tried to set it in firebase due to the keys present in the object. I tried striping out the keys, using JSON.stringify, angular.toJSON but am not having any luck!!

我想知道是否有更清洁或推荐的方法做这个?
感谢如果有人有任何指示或无论如何都可以提供帮助。

I'm wondering is there a cleaner or recommended way to do this? Appreciate If anyone has any pointers or can assist in anyway.

非常感谢,
Noel

Many Thanks, Noel

推荐答案

感谢您的回复。我原来是这样的,我仍然在UID下捕获性别,但我想分开男性和女性。我的应用程序用于约会,所以我认为根据首选的性别属性进行搜索会更有效率。我认为最初搜索的节点减少了约50%。如果您对此有一些建议,我很乐意听到,因为我对firebase / JSON的性能最佳实践不太了解。

Thanks for responding. I had it like this originally and I still capture the gender under the UID but I wanted to separate males and females. My app is for dating so I'm thinking it will be more efficient for doing searches based on the preferred sex attribute. I would think ~50% less nodes to search through initially. If you have some advice on this, would love to hear as I'm not too up on performance best practices for firebase/JSON.

我要发布我用于复制和清理的代码可能对某人有帮助。我在'on'监听器上遇到了问题,它正在删除两个节点。将此更改为一次解决了它。

I'm going to post the code I used for copying and cleaning up as might be helpful to someone. I had problems with 'on' listener, it was deleting both nodes. Changing this to 'once' resolved it.

// firebase refs
    desc.ref1 = new Firebase(desc.userRef+'/'+desc.oldGender+'/'+uid);
    desc.ref2 = new Firebase(desc.userRef+'/'+desc.gender+'/'+uid);

// copy user object
    desc.ref1.once("value", function(snapshot) {

        // copy user data to new gender tree
        desc.ref2.set(snapshot.val(), function(error) {
            if (error) {
                $log.info("could not copy user object." + error);
            } else {
                $log.info("user object copied successfully.");

                // remove user object under old gender
                desc.ref1.remove(desc.onComplete);
            }
        });
    }, function (errorObject) {
        $log.info("The read failed: " + errorObject.code);
    });

这篇关于将Firebase对象移动/复制到另一个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆