Geofire文档说Geofire在读取和写入数据时会返回承诺,但我似乎无法找到任何这方面的例子。我想知道如何在javascript中使用它来将函数附加到在完成geofire承诺时触发的侦听器的末尾。我的代码是:
var onKeyEnteredRegistration = this.geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
}).then(function(result){
console.log("promise resolved with:" + result);
}, function(error){
console.error(error)
});
但是这输出的错误是“未定义不是函数”。引用链式函数。有谁知道如何在Geofire中使用承诺?
由于
答案 0 :(得分:0)
因此,get
类上的set
,remove
和GeoFire
函数仅返回promise。像这样:
geoFire.get("some_key").then(function(location) {
if (location === null) {
console.log("Provided key is not in GeoFire");
}
else {
console.log("Provided key has a location of " + location);
}
}, function(error) {
console.log("Error: " + error);
});
但是,on
函数返回一个GeoQuery
,而on
函数返回一个GeoCallbackRegistration
,该cancel
用于终止var onKeyEnteredRegistration = this.geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});
onKeyEnteredRegistration.cancel(); // the "key_entered" event will stop firing
函数的查询(不返回任何内容。)
$(function(){
//Initial code goes here
});