Firebase 如何在Ionic 2中等待值

Firebase 如何在Ionic 2中等待值,firebase,ionic2,loading,wait,ionic3,Firebase,Ionic2,Loading,Wait,Ionic3,我正在尝试等待在执行“this.bookmarList.take(1).subscribe(…”之后可能更改的值 我的问题是,当我运行应用程序时,它会在执行subscribe之前执行第二个if语句“if(found==false)” 这是我的代码,你可能会发现一些错误或需要添加一些补充来解决我的问题 let loader = this.loadingCtrl.create({ content: "", }); let found=false; loader.present().then(()

我正在尝试等待在执行“this.bookmarList.take(1).subscribe(…”之后可能更改的值

我的问题是,当我运行应用程序时,它会在执行subscribe之前执行第二个if语句“if(found==false)”

这是我的代码,你可能会发现一些错误或需要添加一些补充来解决我的问题

let loader = this.loadingCtrl.create({
  content: "",
});
let found=false;
loader.present().then(()=>{
    this.bookmarkList.take(1).subscribe(data=>{
        data.forEach(b=>{
            if(b.nID==this.appService.id && b.url==url){
                let alert = this.alertCtrl.create({
                title: 'Already Bookmarked',
                    subTitle: 'This note image already bookmarked',
                    buttons: ['OK']
                });
                alert.present();
                found=true;
            }
        })
    })
}).then(()=>{
    if(found==false){
        this.afd.list('/bookmark/').push({
            uemail: this.userService.email,
            nID: this.appService.id,
            url: url,
            date: dateX
        });
        let alert = this.alertCtrl.create({
            title: 'Bookmarked Successfully',
            subTitle: 'This note image has been bookmarked successfully',
                buttons: ['OK']
        });
        alert.present();
    }
}).then(_=>{
    loader.dismiss();
})

我能找到解决办法

let loader = this.loadingCtrl.create({
  content: "",
});
let found=false;
loader.present().then(_=>{
    this.bookmarkList.take(1).subscribe(data=>{
        data.forEach(b=>{
            if(b.nID==this.appService.id && b.url==url){
                let alert = this.alertCtrl.create({
                    title: 'Already Bookmarked',
                    subTitle: 'This note image already bookmarked',
                    buttons: ['OK']
                });
                alert.present();
                found=true;
            }
        })
        if(found==false){
            this.afd.list('/bookmark/').push({
                uemail: this.userService.email,
                nID: this.appService.id,
                url: url,
                date: dateX
            });
            let alert = this.alertCtrl.create({
                title: 'Bookmarked Successfully',
                subTitle: 'This note image has been bookmarked successfully',
                buttons: ['OK']
            });
            alert.present();
        }
        //return
    })
}).then(_=>{
    loader.dismiss();
})

我觉得你的问题不是很清楚。你能把开头的两段改写一下,让它更清楚你需要什么吗?@Chris抱歉,我忘了保存上次编辑的内容。。希望你能抓住我的问题。你为什么不添加
if(find==false){…}
订阅
的内部?@sebaferreras是的,你是对的。@sebaferreras是的,我会。。非常感谢:)