【问题标题】:Parse.Query.each() not including all recordsParse.Query.each() 不包括所有记录
【发布时间】:2015-05-01 23:09:53
【问题描述】:

我在最新的 Parse javascript SDK (v 1.4.2) 中发现了一个错误,该错误 Parse.Query.each() 似乎并没有通过所有匹配的记录。它似乎在某个时间点后停止了。

我有一个存储用户位置 GeoPoints 的 UserLocation 类(在“geo”列中)。我正在尝试查找与场地位置的距离小于 10 英里的用户位置列表。据我了解 Parse.Query.find() 的限制为 1000。根据这篇文章, Parse.Query.each() 没有这样的限制: https://parse.com/questions/pfquery-setskip-does-not-skip-beyond-10000

但从我的测试结果来看似乎并非如此:

mylocation.get("deviceId")
"fa72f38bc0af44f090824a38fd1963b4"
venueGeo.milesTo(mylocation.get("geo"));
2.1965871191375173

如您所见,我所在的位置距离会场位置大约 2 英里。但是如果我运行 10 英里的距离查询,结果没有我的位置:

var locationQuery = new Parse.Query("UserLocation").withinMiles("geo", venueGeo, 10);
results = [] ;
locationQuery.each(function(location) {results.push(location.get("deviceId"))})
b.Promise {_resolved: false, _rejected: false, _resolvedCallbacks: Array[0], _rejectedCallbacks: Array[0], resolve: function…}
results.length
107
results.indexOf("fa72f38bc0af44f090824a38fd1963b4")
-1

但是,如果我在查询中添加额外的约束以返回其 deviceId 等于我所在位置的设备的记录。 Parse.Query.each() 实际上包含它。我想新查询将只返回前一个查询的子集。但它实际上返回了一条不在上一个查询中的记录:

var locationQuery = new Parse.Query("UserLocation").withinMiles("geo", venueGeo, 10).equalTo("deviceId", "fa72f38bc0af44f090824a38fd1963b4");
results = [] ; 
locationQuery.each(function(location) {results.push(location.get("deviceId"))})
b.Promise {_resolved: false, _rejected: false, _resolvedCallbacks: Array[0], _rejectedCallbacks: Array[0], resolve: function…}
results.length
1
results.indexOf("fa72f38bc0af44f090824a38fd1963b4")
0

如果我使用 Parse.Query.limit(1000) 和 Parse.Query.skip() 翻阅记录,我终于在第 8 页找到了我的记录

locationQuery.skip(7000)
locationQuery.find().then(function(locations){
  results = locations.map(function(location){
    return location.get("deviceId");
  })
})
results.indexOf("fa72f38bc0af44f090824a38fd1963b4")
-1

locationQuery.skip(8000)
locationQuery.find().then(function(locations){
  results = locations.map(function(location){
    return location.get("deviceId");
  })
})
results.indexOf("fa72f38bc0af44f090824a38fd1963b4")

927

但分页有其自身的限制,因为您不能跳过超过 10000 条记录,更不用说效率较低了。

这似乎与 Parse javascript 文档说 Parse.Query.each() 没有限制相矛盾。有人遇到过同样的问题吗?

【问题讨论】:

    标签: javascript parse-platform


    【解决方案1】:

    我没有在文档中看到我们被告知 each() 不受 1k 限制的限制,但它不会受到限制是合理的。发布的代码显示了在我看来可能会受到竞争条件的调用和响应,也就是说,在测量结果时可能无法实现每个承诺。这是一个很可能找到记录的确定性测试:

    var locationQuery = new Parse.Query("UserLocation");
    locationQuery.withinMiles("geo", venueGeo, 10);
    results = []; 
    locationQuery.each(function(location {
        results.push(location.get("deviceId"));
    }).then(function() {
        console.log("results length=" + results.length);
        console.log("index of target device id=" + results.indexOf("fa72f38bc0af44f090824a38fd1963b4"));
    });
    

    【讨论】:

    • 感谢您的回复。我的印象是 Parse.Query.each() 不会通过阅读此post "Parse.Query.each() 来强加上限,这将让您获取与查询匹配且没有上限的每个对象。 "我上面粘贴的代码来自 Chrome javascript 控制台。在我真正的 node.js 代码中,我确实有“每个”流的“then”块:return locationQuery.each(function(location){ filteredLocations.push(location); }).then(function(){ return filteredLocations; });
    • 嗯。 Hector 给出了这么多好的答案,所以我猜这是 forEach 没有上限的一个未记录的功能。鉴于此(并且您正在按顺序运行查询),您似乎确实发现了一个合法的错误。我敢打赌,如果您报告了它,他们会很感激.... 一种解决方法是使用find() 运行查询,并通过skip() 进行分页。我应该编辑我的答案来说明,还是你知道怎么做?
    • 我确实提交了一个错误:developers.facebook.com/bugs/103854869948544 看起来问题仅在查询涉及 GeoPoint 时才会发生。跳过有自己的限制(不能跳过超过 10000 条)。所以这对我也不起作用。我确实尝试了 Hector 的使用 orderBy + limit + skip 的建议。这对我也不起作用(至少现在还没有,仍在调试)。最初的印象是 orderBy 在检索记录之后应用,而不是在应用约束之前应用,因此使用 orderBy 结果作为下一批的分隔符不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多