【问题标题】:NativeScript Firebase es6 promises weird behaviourNativeScript Firebase es6 承诺奇怪的行为
【发布时间】: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();
   });
}

结果如下:

  1. 出于某种原因,console.log(result)getValueB 内 函数在 before console.log(result) 内打印 getValueA 函数(为什么??)
  2. this.valueAgetValueB 中是undefined,使我的查询无用
  3. 应用程序崩溃

我的代码有什么问题?我应该使用另一种方法来解决这个问题吗? 提前感谢您对此进行调查:)

【问题讨论】:

    标签: javascript typescript firebase-realtime-database nativescript es6-promise


    【解决方案1】:

    使用 Promise 时,您必须在回调中解析结果。 请在下面找到代码:

    class GetData {
        constructor() {
            this.getValueA()
                .then(resultA => this.getValueB(resultA));
        }
    
        getValueA() {
            return new Promise<string>((resolve, reject) => {
                firebase.query(
                    (result) => {
                        resolve(result.value); // Resolve => returns the result
                    },
                    FirebaseValueAPath, 
                    {
                        singleEvent : true,
                        orderBy : {
                            type: firebase.QueryOrderByType.CHILD,
                            value: 'since' 
                        }
                    }));
            });
        }
    
        getValueB(valueA: string) {
            return new Promise<string>((resolve, reject) => {
                firebase.query(
                    (result) => {
                        resolve(result.value);  // Resolve => returns the result
                    },
                    valueA, 
                    {
                        singleEvent : true,
                        orderBy : {
                            type: firebase.QueryOrderByType.CHILD,
                            value: 'since' 
                        }
                    }));
            });
        }
    }
    

    【讨论】:

    • 这成功了!!谢谢
    • 一个问题……这不被认为是一种反模式吗? stackoverflow.com/questions/23803743/…
    • 我不知道反模式。我学到了一些新东西,谢谢!在分析了您链接中的 github 页面后,我认为我的回答不是反模式。因为firebase.query 不会返回带有结果的Promise。这是一个带有结果回调的函数。因此,如果您想在没有任何外部库的情况下以 Promise 方式使用它,则必须将其包装在 Promise 构造函数中。否则,您可以使用 github 页面中提到的 promisify 库。
    猜你喜欢
    • 2020-01-23
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多