Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying Firebase node with multiple children to another node

Hey I would like to copy the data inside the child node with multiple sub-nodes to another place.

database

In the above image I would like to transfer the data inside the question node into the uid node of users.

In the questions node values I have two field namely correct answer and image url.

I have tried the following

public void copyFirebaseData() {
    DatabaseReference questionNodes = FirebaseDatabase.getInstance().getReference().child("questions");
    final DatabaseReference toUsersQuestions = FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("questions");

    questionNodes.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            toUsersQuestions.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener() { //getting error in this line
                @Override
                public void onComplete(FirebaseError firebaseError, Firebase firebase) {

                    if (firebaseError != null)
                        System.out.println("Copy failed");
                    else
                        System.out.println("Success");

                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}`

But this is throwing an exception

02-24 13:47:09.085 7757-7757/curieo.android.zenmaster E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: curieo.android.zenmaster, PID: 7757
                                                                    com.google.firebase.database.DatabaseException: Failed to parse node with class class curieo.android.zenmaster.mainfragments.HomeFragment$11$1
                                                                        at com.google.android.gms.internal.zzbpf.zza(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbpf.zzar(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbpi.zzas(Unknown Source)
                                                                        at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
                                                                        at curieo.android.zenmaster.mainfragments.HomeFragment$11.onDataChange(HomeFragment.java:750)
                                                                        at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                        at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                        at android.os.Handler.handleCallback(Handler.java:815)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                        at android.os.Looper.loop(Looper.java:207)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5740)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)

How can I save the entire data under questions node and copy it to the uid node. any help would be great

like image 769
Saransh Agarwal Avatar asked Oct 29 '22 11:10

Saransh Agarwal


1 Answers

Though not a very good approach but this approach get the work done.

public void copyFirebaseData() {
    DatabaseReference questionNodes = FirebaseDatabase.getInstance().getReference().child("questions");
    final DatabaseReference toUsersQuestions = FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("questions");

    questionNodes.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot questionCode : dataSnapshot.getChildren()) {
                String questionCodeKey = questionCode.getKey();
                String correctAnswerString = questionCode.child("correctAnswer").getValue(String.class);
                String imageUrlString = questionCode.child("imageUrl").getValue(String.class);
                toUsersQuestions.child(questionCodeKey).child("imageUrl").setValue(imageUrlString);
                toUsersQuestions.child(questionCodeKey).child("correctAnswer").setValue(correctAnswerString);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
like image 139
Saransh Agarwal Avatar answered Nov 15 '22 05:11

Saransh Agarwal