0

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

Irene Serrano
  • 33
  • 1
  • 1
  • 10
  • 2
    Please edit your question and add your database structure as a screenshot. Please also indicate what's the value of `petID`. – Alex Mamo Feb 02 '22 at 07:39
  • Thank you for that but you didn't mention what is the value of `petID` in the code. Have you tried using a hard-coded value instead? Does it work that way? – Alex Mamo Feb 03 '22 at 07:21
  • @AlexMamo is suggesting that you use a hardcoded value for testing purposes. That's part of making a problem as simple as you can for debugging/asking for help. – Ellen Spertus Feb 03 '22 at 20:43
  • You may have a look at this [Stackoverflow case](https://stackoverflow.com/questions/54130061/how-to-update-firestore-subcollection-field). Let me know if that helps! – Mousumi Roy Feb 04 '22 at 13:03
  • @IreneSerrano what exactly are you getting here `document.update({timestamp: time}) //<- line not working`? – Peter Haddad Feb 04 '22 at 23:59
  • @PeterHaddad, the error im getting is unresolved reference: update – Irene Serrano Feb 07 '22 at 18:38

1 Answers1

3

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.

Malik Basit
  • 191
  • 2
  • 10
  • 1
    With this comment and the following [Stack Overflow post](https://stackoverflow.com/a/49682615/14375385) I was able to come up with a working answer – Irene Serrano Feb 08 '22 at 01:53