Firebase startAt String only takes first character

徘徊边缘 提交于 2020-01-02 09:56:34

问题


I have a structure like below under xyz

{
    "pushKey000": {
        "findKey": "john_1",
        "userName": "john",
        "topic": 1
    },
    "pushKey001": {
        "findKey": "john_2",
        "userName": "john",
        "topic": 2
    },
    "pushKey002": {
        "findKey": "joel_1",
        "userName": "joel",
        "topic": 1
    }
}

Now am trying to make a query where I want data of all entries with findKey starting with "john". I tried the following:(Using REST for example)

https://abc.firebaseio.com/xyz.json?orderBy="findKey"&startAt="john"

This gives me all the results including 'joel'. Basically it just uses the first character of startAt, in this case J.

This firebase video fires the same type of query but only searches with just first character.

Is there something wrong that I am doing or is there is any other way to retrieve it using findKey? Thanks a lot for the help in advance

PS: My .indexOn is on findKey and can't change it


回答1:


There is nothing wrong with your code, there is something wrong with your expectations. (I always wanted to write that as an answer :))

The startAt() function works as a starting point for your query, not a filter. So in your case it will find the first occurance of "john" and return everything from that point forward (Including Joel, Kevin, Tim, etc...).

Unfortunatly there is no direct way to do a query where findKey contains the string "john". But luckely there is a (partial) workaround using endAt().

You query will look like this:

orderBy="findKey"&startAt="john"&endAt="john\uf8ff"

Here \uf8ff is the last unicode character (please correct me if I'm wrong).

With this you can query for values that start with "john" like "johnnie", "johnn", "john". But not "1john" or "johm" or "joel".



来源:https://stackoverflow.com/questions/50715093/firebase-startat-string-only-takes-first-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!