Dario Da
Dario Da

Reputation: 37

Firebase query showing wrong data

I have user profiles stored in Firebase with a key "Pricing" and some value saved as integer. I also have profiles that DO NOT have that key.

I execute:

usersReference = Database.database().reference(fromURL: "https://someRef")
    .child("users")
    .queryOrdered(byChild: "Pricing")
    .queryEnding(atValue: pricingToSearch)
    .observe(...)

However, the query result is always showing me also the profiles without that key. How can I exclude them from the search?

Upvotes: 0

Views: 158

Answers (1)

balsick
balsick

Reputation: 1201

You are only telling the query builder to order the data by child 'Pricing' and that the last valid value for 'Pricing' is pricingToSearch. You are not giving it any hint about the first valid value. According to the documentation

Children with a nil value for the specified child key come first.

Your query will probably work if you just add

queryStarting(atValue: 0)

so

usersReference = Database.database().reference(fromURL: "https://someRef")
    .child("users").queryOrdered(byChild: "Pricing")
    .queryStarting(atValue: 0)
    .queryEnding(atValue: pricingToSearch)
    .observe(...)

Upvotes: 2

Related Questions