Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] FirestoreQuery select() method not working correctly, fixed. #424

Open
shafnaz opened this issue Aug 16, 2024 · 3 comments
Open

[BUG] FirestoreQuery select() method not working correctly, fixed. #424

shafnaz opened this issue Aug 16, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@shafnaz
Copy link

shafnaz commented Aug 16, 2024

I got errors trying to use an array in the select() function of FirestoreQuery

func get_top10_total_scores():
# create a query
var query: FirestoreQuery = FirestoreQuery.new()

# FROM a collection
query.from("player_data")

query.select(["total_score", "username"])

# ORDER BY points, from the user with the best score to the latest
query.order_by("total_score", FirestoreQuery.DIRECTION.DESCENDING)
# LIMIT to the first 10 users
query.limit(10)
# Issue the query
# 4.x
var results = await Firebase.Firestore.query(query)

400 Invalid value at 'structured_query.select.fields[0]' (type.googleapis.com/google.firestore.v1.StructuredQuery.FieldReference), "total_score" Invalid value at 'structured_query.select.fields[1]' (type.googleapis.com/google.firestore.v1.StructuredQuery.FieldReference), "username"
I changed it:


# Select which fields you want to return as a reflection from your query.
# Fields must be added inside a list. Only a field is accepted inside the list
# Leave the Array empty if you want to return the whole document

#not working
#func select(fields) -> FirestoreQuery:
	#match typeof(fields):
		#TYPE_STRING:
			#query["select"] = { fields = { fieldPath = fields } }
		#TYPE_ARRAY:
			#for field in fields:
				#field = ({ fieldPath = field })
			#query["select"] = { fields = fields }
		#_:
			#print("Type of 'fields' is not accepted.")
	#return self
#working

func select(fields) -> FirestoreQuery:
	match typeof(fields):
		TYPE_STRING:
			query["select"] = { "fields": [{ "fieldPath": fields }] }
		TYPE_ARRAY:
			var field_list = []
			for field in fields:
				field_list.append({ "fieldPath": field })
			query["select"] = { "fields": field_list }
		_:
			print("Type of 'fields' is not accepted.")
	return self

And it works:

results
[doc_name: sgtW5k40eSosBFBrdRee, 
data: { "total_score": { "integerValue": "100" }, "username": { "stringValue": "pikachu" } }, 
create_time: 2024-08-16T17:52:03.014469Z
, doc_name: Hydf8XQZdXUoiJZGb4S3, 
data: { "total_score": { "integerValue": "31" }, "username": { "stringValue": "abra kadabra" } }, 
create_time: 2024-08-16T17:52:16.806896Z
, doc_name: Sp9LTYyrE9Z7AZlAntSxz8p03YJ3, 
data: { "total_score": { "integerValue": "0" }, "username": { "stringValue": "quartz basilisk 42" } }, 
create_time: 2024-08-12T22:19:50.872262Z
]
@shafnaz shafnaz added the bug Something isn't working label Aug 16, 2024
@WolfgangSenff
Copy link
Collaborator

Hm. Could you open a PR for it? The issue is not the lack of quotes, but rather it seems fields is supposed to be an array, not a dictionary. That was changed recently, but I don't know if I have time to update it anytime soon.

@shafnaz
Copy link
Author

shafnaz commented Aug 16, 2024

Sure.

@shafnaz
Copy link
Author

shafnaz commented Aug 16, 2024

#425

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants