Irene Serrano
Irene Serrano

Reputation: 63

Firebase Firestore: Update a field of a document in a subcollection while retrieving information

How can I update a field to a document, while getting the the fields of that document? I am unsure how to do so given my setup. It appears that I can't use .update() with my code as I get an unresolved reference: update error.

class ReminderActivity : AppCompatActivity() {
    lateinit var reminderID: String
   
    val db = Firebase.firestore
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reminder)

        val completeTask: Button = findViewById(R.id.complete_button)
        completeTask.setOnClickListener {
            db.collection("pets").document("vNkkDGAS1oBMpanEXhte").collection("reminders").document(iFdIMKSxJV79Mm64OHwp).get().addOnSuccessListener { document ->
                    val timeStamp = document.get("timestamp") as com.google.firebase.Timestamp
                    val millisec = timestamp.seconds * 1000 +timestamp.nanoseconds/1000000
                    val netDate = Date(millisec)
                    val frequency = document.get("frequency").toString()
                    val cal: Calendar = Calendar.getInstance()
                    cal.time = netDate
                    
                    val time = Timestamp(cal.timeInMillis)
                    document.update({timestamp: time}) //<- line not working
                }
        }
    }
}

pictures of cloud firestore database enter image description here

enter image description here

enter image description here

Upvotes: 0

Views: 2633

Answers (1)

Malik Basit
Malik Basit

Reputation: 191

I believe document is of type DocumentSnapshot class and you can't update with this object. Update can only be done from DocumentReference class object. You need to update the implementation for updating the document.

Upvotes: 3

Related Questions