QML Firebase startAt returns undefined

228 Views Asked by At

I am working on a 'typeahead’ type function which will check my Database with the current typed text to provide search suggestions of users using Felgo.

Here is the link for Felgos Firebase documentation

As to not search every entry I am looking to use the startAt and limitTo for a lower data use.

However when applying the startAt my searches only return undefined, I have tried testing this by changing my startAt from a variable to explicit data but this still only returns undefined.

My function is below:

function searchUsers(searchString) {
    db.getValue("public/nameList/", {
        orderByChild: true,
        startAt: searchString,      //searchString is a variable with my .currentText to search.
        limitToFirst: 10,
        }, function(success, key, value) {
               if(success) {
               searchArr = []
               searchArr = value
               console.debug("Read user value for key", key, "from DB:", value)
               }
           })
}

I have also tried by passing my var searchString through JSON.stringify(searchString) and also return undefined!

Removing the startAt: query entirely returns the entire result of nameList as expected, but no matter how I try to implement my startAt it always returns undefined.

A sample of my nameList JSON is:

nameList: {
  "EddieLaw245" : 530343772383,
  "EddieLawrence91" : 530343772385,
  "EdwardL91" : 530343772386,
  "EdwardLaw" : 530343772384,
  "Edwardlawrence91" : 530343772380,
  "JoBrownLondon" : 530343772381,
  "KatiePrescottHair" : 543592635596,
  "Tracey-Sweeting" : 530343772382
}

So with the above example, When I type E it should remove the last 3 entries, and so on.

1

There are 1 best solutions below

9
Frank van Puffelen On BEST ANSWER

The problem is that you're specifying orderByChild: true. If we look at the documentation of that:

orderByChild: If present, the queried object will have its properties ordered by values at sub-paths defined by the value of this property. Ordering by child properties makes the filter properties startAt, endAt and equalTo filter by the child property values

It may not be immediately clear from this, but orderByChild allows you to order the results on a property value under each of those nodes. So your code tries to order the child nodes on the value of a property true, which isn't possible (and should actually generate a compile-time error in the library) as the nodes under nameList don't have any child properties of their own. They merely have a key and a value.

What you're looking for is orderByKeys, which orders the child nodes on their keys. So:

db.getValue("public/nameList/", {
    orderByKeys: true,
    startAt: searchString,
    limitToFirst: 10,
}

You'll typically also want to specify an endAt value, to ensure your type-ahead only shows values that start with the search string. If you only allow ASCII values in the keys, the simplest way to do this is:

  startAt: searchString,
  endAt: searchString + "~",

The ~ here is no magic operator, but merely the last ASCII characters. If you want to allow a broader character set, you'll need to use the last character in that character set - for example \uF7FF is the last code point for Unicode.

Update from OP Though I'm certian Franks correct with typical Firebase usage; I suspect due to the Felgo plugin I am using the full solution has a slight adjustment;

db.getValue("public/nameList/", {
    "orderByKey": true,
    "startAt": searchString,
    "endAt": searchString+"~",
    "limitToFirst": 10,
    }, function(success, key, value) {....}
})

Notes on the above - my filters/queries are surrounded by quotation marks "startAt", also instead of orderByKeys, I have used orderByKey