Angular 2 Firebase Observable承诺不返回任何内容 [英] Angular 2 Firebase Observable to promise doesn't return anything

查看:81
本文介绍了Angular 2 Firebase Observable承诺不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用AngularFire2进行Angular 2项目,并且正在尝试将FirebaseListObservable转换为Promise.我知道这没有多大意义,因为Observables更有用,但是此功能将是另一个链接了多个promise的功能的一部分.而且我不熟悉如何在promise链中订阅Observables ...该函数在服务中执行,但是似乎不返回任何内容. 基本上,我想做的是检查Firebase列表中是否存在具有特定名称的对象,并返回true或false.

I'm currently working on an Angular 2 project with AngularFire2 and I'm trying to convert a FirebaseListObservable to a Promise. I know that it doesn't make much sense since Observables are more useful but this function will be part of another function where multiple promises are chained. And I'm not familiar with how to subscribe to Observables within a chain of promises... The function gets executed in the service, however it seems to not return anything. Basically, what I want to do is check in a Firebase list if an object with a certain name already exists and return either true or false.

constructor(private _af: AngularFire) { }

nameExists(name: string): Promise<boolean> {

 return this._af.database.list('users')
  .map(users => {

    let exists = false;
    users.forEach(user => {
      if(user.name.toLowerCase() === name.toLowerCase()) {
        console.log('Name already exists!');
        exists = true;
      }
    });
    return exists;
  }).toPromise();
}

组件

constructor(private _usersService: UsersService) { }

check(name) {
 this._usersService.nameExists(name)
  .then(bool => console.log(bool));
}

因此,当存在匹配项时,该函数将被执行并在打印到控制台时似乎可以正常工作.但是,该组件中的console.log()不会执行.我猜那"部分永远都不会达到.另外,一旦找到匹配项,是否有办法停止forEach循环?

So the function gets executed and seems to work correctly as it prints to the console, when there's a match. However, console.log() in the component does not get executed. I guess the "then" part is never reached. On a separate note, is there a way to stop the forEach loop once a match is found?

任何帮助将不胜感激,因为我根本找不到任何答案.

Any help would be greatly appreciated as I couldn't find any answers at all to this.

推荐答案

问题在于toPromise运算符将可观察对象转换为解析为可观察对象的最终值的承诺.这意味着可观察者必须在诺言解决之前完成.

The problem is that the toPromise operator converts the observable to a promise that resolves to the observable's final value. That means the observable must complete before the promise resolves.

在AngularFire2中,列表和对象可观察对象不完整;每当数据库更改时它们都会重新发出.

In AngularFire2, list and object observables don't complete; they re-emit whenever the database changes.

您可以使用first运算符解决该问题,该运算符采用第一个发出的值,然后完成所组成的可观察项:

You can solve the problem using the first operator, which takes the first emitted value and then completes the composed observable:

import 'rxjs/add/operator/first';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
...
return this._af.database
  .list('users')
  .map(users => {

    let exists = false;
    users.forEach(user => {
      if (user.name.toLowerCase() === name.toLowerCase()) {
        console.log('Name already exists!');
        exists = true;
      }
    });
    return exists;
  })
  .first()
  .toPromise();

这篇关于Angular 2 Firebase Observable承诺不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆