Home > Blockchain >  Why can't I access my data from Firestore?
Why can't I access my data from Firestore?

Time:05-30

I'm trying to display data from Firestore and add it to a PieChart. I can't figure out why I can't access my data

This is how data are stored in Firestore:

enter image description here

This is how I try to access it:

 private val mFirestore = FirebaseFirestore.getInstance()
var chartdata: ArrayList<Measurements> = ArrayList()
private var chart: ScatterChart? = null

fun getCurrentUserID(): String {

    val currentUser = FirebaseAuth.getInstance().currentUser

    var currentUserID = ""
    if (currentUser != null) {
        currentUserID = currentUser.uid
    }

    return currentUserID

}
 mFirestore.collection(Constants.MEASUREMENTS)
            .whereEqualTo(Constants.USER_ID, getCurrentUserID())
            .get()
            .addOnSuccessListener { queryDocumentSnapshots ->
                val userdata : ArrayList<Measurements> = ArrayList()
                val weekdata = ArrayList<Measurements>()
                if (!queryDocumentSnapshots.isEmpty) {
                    for (journals in queryDocumentSnapshots) {
                        val displayData: Measurements = journals.toObject(Measurements::class.java)
                        userdata.add(displayData)
                        Log.e("Data for chart", journals.toString())
                    }

And I get this error:

enter image description here

CodePudding user response:

The data is being fetched precisely that's why you can see all the document names in the logcat but as you are logging DocumentSnapshot object, that's why you are seeing the data in unusual format. Try logging displayData variables like:

Log.d("Data for chart", displayData.activity) // Use Log.d instead of Log.e

or userdata as an array and it will work as desired.

  • Related