0
我试图链接2使用承诺的Firebase查询,因为我需要第一个查询的结果来获得第二个。这里是2所查询的结构:NativeScript Firebase ES6承诺奇怪的行为
查询值:
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();
});
}
其结果如下:
- 对于内部getValueB 功能因故
console.log(result)
被内部 以前的console.log(结果)打印getValueA功能(为什么?) this.valueA
是getValueB
undefined
,使我的查询没用- 应用程序崩溃
什么是错的机智我的代码?我应该使用另一种方法来解决这个问题吗? 预先感谢您对此进行调查:)
这样做了!谢谢 –
虽然......有一个问题不是被认为是反模式吗? http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it –
我不知道反模式。我学到了一些新东西,谢谢!在分析链接中的github页面之后,我不认为我的答案是反模式。因为'firebase.query'不会返回带有结果的'Promise'。这是一个带结果回调的函数。所以,如果你想以Promise的方式使用它,而没有任何EXTERNAL LIBRARY,你必须将它包装在'Promise'构造函数中。否则,你可以像github页面中提到的那样使用'promisify'库。 – Guillaume