Error while performing compound queries in firestore - firebase

I want to perform a compound query in firestore where I would like to get all documents with field bloodgroup equal to A+ and with field createdBy not equal to email. This email is that of the logged in user. When I perform the query I get NullPointerException. How to perform the query correctly 021-07-24 19:50:24.746 17550-17550/com.example.bloodbankcompany E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bloodbankcompany, PID: 17550 java.lang.NullPointerExceptionatcom.example.bloodbankcompany.UserlistActivity$EventChangeListener3$1.onEvent(UserlistActivity.kt:217) I am storing the document snapshot inside the userArrayList array. Without the whereNotEqualTo query I am getting output where my documents get listed in recyclerview.
private fun EventChangeListener2(){
val sharedPreferences1 = getSharedPreferences("email", Context.MODE_PRIVATE)
val email: String? = sharedPreferences1.getString("email","null")?.trim()
Toast.makeText(this, "ssrae$email", Toast.LENGTH_SHORT ).show()
mFireStore.collection("applicationForm").whereNotEqualTo("createdBy",email).whereEqualTo("bloodgroup","A+").addSnapshotListener(object : EventListener<QuerySnapshot>{
override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
if (error!= null){
Log.e("firestore error", error.message.toString())
}
for(dc: DocumentChange in value?.documentChanges!!){
if (dc.type== DocumentChange.Type.ADDED){
userArrayList.add(dc.document.toObject(User1::class.java))
var number=userArrayList
var number1 =userArrayList
}
// Toast.makeText(applicationContext,userArrayList.toString(), Toast.LENGTH_SHORT).show()
}
myAdapter.notifyDataSetChanged()
}
})
}

Well, I have edited a little bit of your code, if it still doesn't work add a comment.
Also, an explanation about changes is commented below.
private fun EventChangeListener2() {
val sharedPreferences1 = getSharedPreferences("email", Context.MODE_PRIVATE)
val email: String? = sharedPreferences1.getString("email", "null")?.trim()
Log.d("firestore email", email.toString())
Toast.makeText(this, "ssrae$email", Toast.LENGTH_SHORT).show()
// try and catch will avoid your app to crash.
try {
//ref
var ref = mFireStore.collection("applicationForm")
.whereEqualTo("bloodgroup", "A+")
/**
* I believe since your email is of type nullable and there may be
* maybe a chance that email is null and is given to whereNotEqualTo
* I am just making an assumption here since I don't know what you
* recieve from sharedPreferences and whether it is null or not
*
* So, what I have done here is,
* firstly, I have split the firestore call into 2 parts and
* Secondly, I have a null-check for email, if it is
* not-null ref will also include this query
*
*/
//null Check for email
if (email != null) ref = ref.whereNotEqualTo("createdBy", email)
// Snapshot Listener
ref.addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
if (error != null) {
Log.e("firestore error", error.message.toString())
}
for (dc: DocumentChange in value?.documentChanges!!) {
if (dc.type == DocumentChange.Type.ADDED) {
userArrayList.add(dc.document.toObject(User1::class.java))
var number = userArrayList
var number1 = userArrayList
}
// Toast.makeText(applicationContext,userArrayList.toString(), Toast.LENGTH_SHORT).show()
}
myAdapter.notifyDataSetChanged()
}
})
} catch (e: Exception) {
Log.e("firestore error", "Error", e)
}
}
EDIT:
According to firebase docs,
https://firebase.google.com/docs/firestore/query-data/indexing#exemptions
If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Related

Adding document İD to firebase document as a field

I have a model with default values. My app gets the data from user through EditTexts and add them to Firebase Firestore. I hava an addData function (in AddAnalyzeActivity) and savefunction (in AddAnalyzeViewModel) for this operation. I'm getting EditText entries in AddAnalyzeActivity and adding them to my model but on this step ı want to add document id to my model but I can't access the documentIds properly in AddAnalyzeActivity. I can only access them with a forEach method when I try to retrieving the mentioned data with retrieveData function (in PairDetailVM) from Firestore but If I try to add document Ids in retrieveData method it only adds default value of documentId.
What I tried to:
Using #DocumentId annotation in my model.
Setting null default value of documentId in my model.
Getting a list of all documents' ids but can't match them with actual items.
Here is the screenShot for logic:
AnalyzeModel:
data class AnalyzeModel(
var concept: String?="",
var reason: String?="",
var result: String?="",
var rrRatio: Double?=0.0,
var tarih: Timestamp=Timestamp.now(),
var tradingViewUrl: String="",
var id : String="")
addData :
fun addData(view: View) {
val tarih = com.google.firebase.Timestamp.now()
val rr = rrText.text.toString()
var doubleRR = rr.toDoubleOrNull()
if (doubleRR == null) { doubleRR = 0.0 }
val analyzeDTO = AnalyzeModel(
conceptText.text.toString(),
reasonForText.text.toString(),
resultAddingText.text.toString(),
doubleRR,
tarih,
chartImage.text.toString()
)
viewModel.save(analyzeDTO)
val intent = Intent(this, PairDetailActivity::class.java)
startActivity(intent)
finish()
}
save :
fun save(data: AnalyzeModel) {
database.collection(dbCollection!!).document("Specified").collection("Pairs")
.document(chosenPair!!)
.collection("Analysis")
.add(data)
.addOnFailureListener { exception ->
exception.printStackTrace()
Toast.makeText(getApplication(), exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}
retrieveData:
private fun retrieveData() {
val docRef = collectionRef.orderBy("tarih", Query.Direction.DESCENDING)
docRef.addSnapshotListener { value, error ->
try {
if (value != null && !value.isEmpty) {
val allAnalysis= ArrayList<AnalyzeModel>()
val documents = value.documents
documents.forEach {
val analyze = it.toObject(AnalyzeModel::class.java)
if (analyze!=null){
allAnalysis.add(analyze)
}
}
list.value = allAnalysis
} else if (error != null) {
Toast.makeText(Application(), error.localizedMessage, Toast.LENGTH_LONG).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
If you want to save the document ID into the document itself, consider separating the creation of the new DocumentReference from writing to it, by using set instead of add.
fun save(data: AnalyzeModel) {
val newRef = database.collection(dbCollection!!).document("Specified").collection("Pairs")
.document(chosenPair!!)
.collection("Analysis")
.document() // 👈 generates a new reference with a unique ID
data.id = newRef.id // 👈 set the ID into your object
newRef.set(data) // 👈 writes the data to the new reference
.addOnFailureListener { exception ->
exception.printStackTrace()
Toast.makeText(getApplication(), exception.localizedMessage, Toast.LENGTH_LONG).show()
}
}
Also see the second snippet in the documentation on adding a document

Checking if username already exists gives error kotlin

I translated the code below from java, but I am getting an error in some places, why is it giving an error, is there a point I missed?
val mQuery: Query = firestore.collection("users")
.whereEqualTo("nickname", mUserName)
mQuery.addSnapshotListener(object : EventListener<QuerySnapshot> {
fun onEvent(
documentSnapshots: QuerySnapshot,
e: FirebaseFirestoreException?
) {
for (ds in documentSnapshots) {
if (ds != null) {
val userName: String = document.getString("username")
Log.d(
TAG,
"checkingIfusernameExist: FOUND A MATCH: $userName"
)
Toast.makeText(
this#SignUpActivity,
"That username already exists.",
Toast.LENGTH_SHORT
).show()
}
}
}
})
I've been doing the things described here for 2-3 days, but it keeps throwing an error.Event listeners and docs throw errors.
I translated the code below from java
That's not the correct way of "translating" that code from Java to Kotlin, since that answer provides a solution for getting data only once. So to be able to do that in Kolin programming language please use the following lines of code:
val rootRef = FirebaseFirestore.getInstance()
val allUsersRef = rootRef.collection("all_users")
val userNameQuery = allUsersRef.whereEqualTo("username", "userNameToCompare")
userNameQuery.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
for (document in task.result) {
if (document.exists()) {
Log.d("TAG", "username already exists")
val userName = document.getString("username")
//Do what you need to do with the userName
} else {
Log.d("TAG", "username does not exists")
}
}
} else {
Log.d("TAG", "Error getting documents: ", task.exception)
}
}

Firestore pagination with Jetpack Paging 3 - startBefore not loading the correct page

This is my attempt at paginating a collection of Firestore chat messages with Paging 3. It correctly loads the next pages, so the startAfter operator seems to be working as expected. But it's not loading previous pages correctly. Instead, it always loads the very first page again and appends it at the beginning of the list. (I start dropping pages after 100 items so lazy loading works in both directions).
The prevKey seems to be passed correctly. It has the correct value at beginning of the load method right before we build the query.
timeStamp is a Firestore server timestamp annotated with #ServerTimestamp if that matters.
class ChatMessagesPagingSource(
private val messageCollection: CollectionReference
) : PagingSource<ChatMessagesPagingSource.PagingKey, ChatMessage>() {
override suspend fun load(params: LoadParams<PagingKey>): LoadResult<PagingKey, ChatMessage> {
return try {
var query = messageCollection
.orderBy("timeStamp", Query.Direction.DESCENDING)
.limit(params.loadSize.toLong())
val key = params.key
Timber.d("key = $key")
query = when (key) {
is PagingKey.PreviousKey -> query.endBefore(key.endBefore)
is PagingKey.NextKey -> query.startAfter(key.startAfter)
null -> query
}
val querySnapshot = query.get().await()
val chatMessages = querySnapshot.toObjects(ChatMessage::class.java)
val firstDoc = querySnapshot.documents.firstOrNull()
val lastDoc = querySnapshot.documents.lastOrNull()
val prevKey = if (firstDoc != null) PagingKey.PreviousKey(firstDoc) else null
val nextKey = if (lastDoc != null) PagingKey.NextKey(lastDoc) else null
Timber.d("first message: ${chatMessages.firstOrNull()}")
Timber.d("last message: ${chatMessages.lastOrNull()}")
LoadResult.Page(
data = chatMessages,
prevKey = prevKey,
nextKey = nextKey
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
sealed class PagingKey{
data class PreviousKey(val endBefore: DocumentSnapshot) : PagingKey()
data class NextKey(val startAfter: DocumentSnapshot) : PagingKey()
}
}
You need to check for the type of LoadParams, to see if it is refresh, prepend or append.
Since your query always fetches items after in descending order, when it requests a prepend you're probably loading the incorrect items.

How to retrieve a field in firebase with user id

I am implementing a quiz app in Android studio using kotlin. I am communicating with firebase where I store users who signs up. This is how it is structured in firebase:
And this is how I save a user when they signed up:
class User(val uid: String, val Username: String, var highscore: Int)
private fun saveUserToDatabase(){
val uid: String? = FirebaseAuth.getInstance().uid
val ref: DatabaseReference = FirebaseDatabase.getInstance().getReference( "/Users/$uid")
val user = User(uid.toString(), signUpUsername.text.toString(), 0)
ref.setValue(user)
.addOnSuccessListener {
Log.d("RegisterActivity", "User was saved in the database")
}
}
}
When a user has logged in and have played a round I want to retrieve the highscore with the current users userId and update the highscore if the score is greater than the highscore. This is my function that handles that:
private fun updateScore(score: Int, userId: String, isLoggedIn: Boolean){
if(isLoggedIn) {
var ref = FirebaseDatabase.getInstance().reference
ref.child(userId).addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
Log.e("onCancelledError", "onCancelled", error.toException())
}
override fun onDataChange(dataSnapShot: DataSnapshot) {
var highscore = dataSnapShot.child("highscore").getValue(Int::class.java)
Log.d("highscore", highscore.toString())
if(highscore != null){
if (highscore < score) {
highscore = score
dataSnapShot.ref.setValue(highscore)
}
}
}
})
}
}
But this does not work because highscore is null, when it should be 0. My userId is right so it is not that, I do not know what is wrong. Since I am new to firebase I have a hard time understanding the syntax. Any suggestions what could be wrong?
Thanks!
You're not building the reference to the user's node correctly. You probably meant to do this:
ref.child('Users').child(userId).addListenerForSingleValueEvent(...)
Note child('Users').

Cloud Firestore returns false when getting an existing document

I'm trying to retrieve a document by a specific id passed by parameter and I'm 100% sure the document exists but Firestore says the opposite.
Here's the code of my function
val db = FirebaseFirestore.getInstance()
private fun retrieveUsersFromParticipantsId(participant: String) {
db.collection("Usuarios").document(participant).get().addOnCompleteListener {
if (it.isSuccessful) {
val user = it.result.toObject(User::class.java)
participantsArray.add(user!!)
}
}
}

Resources