【发布时间】:2016-09-27 22:09:37
【问题描述】:
我正在尝试使用 Promise 链接 2 个 Firebase 查询,因为我需要第一个查询的结果来获得第二个。这是 2 个查询的结构:
值 A 的查询:
private getValueA(){
var queryEvent = (result):void =>{
console.dump(result);
this._valueA = result.value;
}
return firebase.query(
queryEvent,
FirebaseValueAPath,
{
singleEvent : true,
orderBy : {
type: firebase.QueryOrderByType.CHILD,
value: 'since'
}
});
}
值 B 的查询:
private getValueB{
let FirebaseValueBPath :string = this._valueA
var queryEvent = (result) :void =>{
console.dump(result);
if(result.value){
this._valueB = result.value;
}
}
return firebase.query(
queryEvent,
FirebaseValueBPath,
{
singleEvent : true,
orderBy : {
type : firebase.QueryOrderByType.CHILD,
value : 'since'
}
});
}
}
然后我尝试通过执行以下操作将它们链接在一起:
constructor(){
this.getValueA().then(
(success) :void => {
this.getValueB();
});
}
结果如下:
- 出于某种原因,
console.log(result)在 getValueB 内 函数在 before console.log(result) 内打印 getValueA 函数(为什么??) -
this.valueA在getValueB中是undefined,使我的查询无用 - 应用程序崩溃
我的代码有什么问题?我应该使用另一种方法来解决这个问题吗? 提前感谢您对此进行调查:)
【问题讨论】:
标签: javascript typescript firebase-realtime-database nativescript es6-promise