1

So I store a user with field called myMovies in firestore. I am able to successfully add a movie in this array but when I try to remove one it does not work and the logic behind the adding and removing is similar.

It does show me that the array is successfully updated but in firestore the element I delete is still there.

Here is my code:

 @Override
public void removeMovie(Movie movie, FirebaseAddMovieListener listener) {
    firebaseFirestore.collection("users").document(Objects.requireNonNull(mAuth.getCurrentUser()).getUid()).update("myMovies", FieldValue.arrayRemove(movie))
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    listener.onSuccess("SUCCESS");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    listener.onFailure(e.getMessage() + e + e.getLocalizedMessage());
                }
            });

}

Add Movie

 @Override
   public void addMovieToUserLibrary(Movie movie, FirebaseAddMovieListener 
listener) {

    firebaseFirestore.collection("users").document(Objects.requireNonNull(mAuth.getCurrentUser()).getUid()).update("myMovies", FieldValue.arrayUnion(movie))
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    listener.onSuccess("SUCCESS");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    listener.onFailure(e.getMessage() + e + e.getLocalizedMessage());
                }
            });

}

Database Structure database

Fiphe
  • 320
  • 3
  • 18
  • Should "movie" not be in " " ?, But the code looks fine, maybe is where you are executing the method, also check your spelling for your collection and such – Ruben Meiring Jun 22 '20 at 12:05
  • Please edit your question and add your database structure as a screenshot. Besides that, what are the values of `Objects.requireNonNull(mAuth.getCurrentUser()` and `movie`? Please respond with @AlexMamo – Alex Mamo Jun 22 '20 at 12:05
  • @AlexMamo I have added the database structure, also movie is an object I want to delete and getCurrentUser is the user who is currently logged in, I want to delete a movie from his list of movies. – Fiphe Jun 22 '20 at 12:25
  • @RubenMeiring spelling is all correct, I have the same logic for arrayUnion when I add an object and it works fine. – Fiphe Jun 22 '20 at 12:26
  • Can you post the arrayUnion Code so we can compare them might see the issue there – Ruben Meiring Jun 22 '20 at 12:37
  • @RubenMeiring yes, just did it. – Fiphe Jun 22 '20 at 12:40
  • I can see no problem with your code, did you check your error log? – Ruben Meiring Jun 22 '20 at 12:47
  • There is nothing there. – Fiphe Jun 22 '20 at 12:48
  • You are storing objects within that array. So please check the duplicate to see how you can remove an item from an array of objects. – Alex Mamo Jun 22 '20 at 13:20
  • 2
    @AlexMamo The duplicate you marked is removing by array index, while OP here seems to be using `Movie` as far as I can see. – Frank van Puffelen Jun 22 '20 at 14:39
  • 1
    @Fiphe: can you show how you populate the `movie` to remove it? – Frank van Puffelen Jun 22 '20 at 14:40
  • @FrankvanPuffelen Oh, yes. Looking forward to seeing OP's answer. – Alex Mamo Jun 22 '20 at 14:51
  • Please add what Frank van Puffelen asked for and please also respond with @AlexMamo – Alex Mamo Jun 22 '20 at 14:51
  • I have found the issue. Even though it did not show me any exception while checking my code I saw that one of my fields in the model class had a capital letter while the rest had small letters. That fixed it, though, adding an object to the array worked even with the capital letter which is strange. – Fiphe Jun 22 '20 at 22:32

0 Answers0