简体   繁体   English

QML Firebase startAt 返回未定义

[英]QML Firebase startAt returns undefined

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.我正在使用“预先输入”类型 function,它将使用当前键入的文本检查我的数据库,以提供使用 Felgo 的用户的搜索建议。

Here is the link for Felgos Firebase documentation这是Felgos Firebase 文档的链接

As to not search every entry I am looking to use the startAt and limitTo for a lower data use.至于不搜索每个条目,我希望使用startAtlimitTo来减少数据使用。

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.但是,当应用startAt时,我的搜索只返回未定义的,我尝试通过将我的 startAt 从变量更改为显式数据来测试它,但这仍然只返回未定义的。

My function is below:我的function如下:

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!我也尝试通过JSON.stringify(searchString)传递我的var searchString并返回未定义!

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.删除startAt:查询完全按预期返回nameList的整个结果,但无论我如何尝试实现我的startAt ,它总是返回未定义。

A sample of my nameList JSON is:我的姓名列表nameList的示例是:

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.因此,对于上面的示例,当我键入E时,它应该删除最后 3 个条目,依此类推。

The problem is that you're specifying orderByChild: true .问题是您指定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. orderByChild:如果存在,查询的 object 的属性将按此属性值定义的子路径中的值排序。 Ordering by child properties makes the filter properties startAt, endAt and equalTo filter by the child property values按子属性排序使过滤器属性 startAt、endAt 和 equalTo 按子属性值过滤

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.从中可能无法立即清楚,但orderByChild允许您对每个节点的属性值的结果进行排序。 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.因此,您的代码尝试根据属性true的值对子节点进行排序,这是不可能的(并且实际上应该在库中生成编译时错误),因为nameList下的节点没有它们的任何子属性自己的。 They merely have a key and a value.他们只有一个键和一个值。

What you're looking for is orderByKeys , which orders the child nodes on their keys.您正在寻找的是orderByKeys ,它根据键对子节点进行排序。 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.您通常还需要指定一个endAt值,以确保您的预输入仅显示搜索字符串开头的值。 If you only allow ASCII values in the keys, the simplest way to do this is:如果您只允许在键中使用 ASCII 值,最简单的方法是:

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

The ~ here is no magic operator, but merely the last ASCII characters.这里的~不是魔术运算符,而只是最后一个 ASCII 字符。 If you want to allow a broader character set, you'll need to use the last character in that character set - for example is the last code point for Unicode.如果您想允许更广泛的字符集,则需要使用该字符集中的最后一个字符 - 例如是 Unicode 的最后一个代码点。

Update from OP Though I'm certian Franks correct with typical Firebase usage;从 OP 更新虽然我是 certian Franks 正确的典型 Firebase 用法; I suspect due to the Felgo plugin I am using the full solution has a slight adjustment;我怀疑由于我使用的 Felgo 插件,完整的解决方案有轻微的调整;

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上面的注释——我的过滤器/查询被引号"startAt"包围,也代替了orderByKeys ,我使用orderByKey

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM