Skip to content

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

@shafnaz

Description

@shafnaz

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
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions