You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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()
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:
And it works:
The text was updated successfully, but these errors were encountered: