Angular 2 Firebase Observable to promise doesn't return anything

Leonidas

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.

Service

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();
}

component

constructor(private _usersService: UsersService) { }

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

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.

cartant

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.

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

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();

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

postgresql trigger with dblink doesn't return anything

分類Dev

Post Request with angular doesn't return anything from my Action Method

分類Dev

Firebase query does not return anything

分類Dev

firebase return onSnapshot promise

分類Dev

Logitech MX Anywhere 2 mouse pairs but doesn't do anything

分類Dev

Logitech MX Anywhere 2 mouse pairs but doesn't do anything

分類Dev

Angular 6 return Observable

分類Dev

Angular Promise Return

分類Dev

Angular 2 Observables - Return original observable, instead of switchMap/combineLatest

分類Dev

how to return observable from function that has a promise

分類Dev

Angular2コールバックとPromise / Observable

分類Dev

Angular - APP_INITIALIZER - Promise vs Observable

分類Dev

How to locate an error in an Angular promise or observable?

分類Dev

Angular 2 temlate with Observable

分類Dev

express-validator doesn't do anything

分類Dev

AlamoFire doesn't appear to be doing anything

分類Dev

Array doesn't print anything on executing code

分類Dev

Grunt Smoosher doesn't do anything

分類Dev

PHP SimpleXML doesn't output anything

分類Dev

Collectionview doesn't show anything during runtime

分類Dev

Why doesn't ZIP Compression compress anything?

分類Dev

btrfs filesystem resize doesn't do anything

分類Dev

Observable doesn't receives events

分類Dev

Using promise with observable in ionic 2 applications

分類Dev

Angular 2ObservableからObservable []

分類Dev

Angular2 ChangeDetection or Observable?

分類Dev

Firebase Auth REST API - signing in with email/password doesn't return refresh token

Related 関連記事

ホットタグ

アーカイブ