Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

android - How to remove value from HashMap (No properties to serialize found)?

I've got this structure of the database: enter image description here

-requests
    -userID
        -requests
            -requestingUserID1 : groupID1
            -requestingUserID2 : groupID2

How to delete specific request by HashMap's key? Let's say I have some requestingUserID, and I want to delete it. So far I've got:

val updates = HashMap<String, Any>()
// updates["/requests/${firebaseUser.uid}/friendId"] = FieldValue.delete() // verion 1
updates["/requests/${firebaseUser.uid}/requests.${friendId}}"] = FieldValue.delete() //version 2
// more updates

db
    .updateChildren(updates) // error occurs here
    // onCompleteListener()

I get the following error:

com.google.firebase.database.DatabaseException: No properties to serialize found on class com.google.firebase.firestore.FieldValue$DeleteFieldValue
question from:https://stackoverflow.com/questions/65881089/how-to-remove-value-from-hashmap-no-properties-to-serialize-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The simplest solution to delete a record from a Firebase Realtime Database is to use "removeValue()" method as shown in the following lines of code:

val rootRef = FirebaseDatabase.getInstance().getReference()
val friendIdRef = rootRef.child("requests/${firebaseUser.uid}/requests/${friendId}")
friendIdRef.removeValue().addOnCompleteListener(object : OnCompleteListener<Void?>() {
    fun onComplete(task: Task<Void?>) {
        if (task.isSuccessful()) {
            Log.d(TAG, "Item successfully deleted.")
        }
    }
})

The delete() method that you are using is apart of the Firestore SDK. While both databases are apart of Firebase, both are two different products, with two different mechanisms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...