What’s difference between Firebase.firestore
and FirebaseFirestore.getInstance()
?
When do I want to update DB, which one is appropriate for this purpose?
And when I want to check the extracted result value from Firestore.
if I log it, it only prints the object name not result in string value that is contained in the object.
db.collection("users").whereEqualTo("userId", userId?.toInt())
.get()
.addOnSuccessListener {
documents - >
for (document in documents) {
Log.d("결과", documents.toString())
}
}
If I print this, it throws result like below:
com.google.firebase.firestore.QuerySnapshot@b38fd47b
I want to read fields and values of this QuerySnapshot, how can I do this?
share code and error:
var db = Firebase.firestore
val stDocRef = db.collection("users").whereEqualTo("userId", userId?.toInt())
db.runTransaction { transaction ->
transaction.update(stDocRef,"name", "new name")
null
}.addOnSuccessListener { Log.d("결과", "Transaction success!") }
2
Answers
You are trying to print the
QuerySnapshot
Object , Hence it is giving you theMemory Address of the Object
. To overcome this, try to printdocument.data
as show in the below example.Refer the following links for CRUD operation in firebase with kotlin :
None. Both are creating the same instance. The first instance is created by using an extension property of the Firebase class, while the second one is creating an instance using the getInstance() method of the FirebaseAuth. The latter one is also a singleton.
That’s the expected behavior, since your calling
toString()
on thedocuments
object:The
documents
that comes from the lambda expression is actually a document of type QuerySnapshot which:So you have to loop through the documents, as you already and get the data according to the type of the field:
If you however want to run a transaction, which in my opinion doesn’t make any sense, since you’re only reading data, then please check the documentation, or my answer from the following post: