Angular 2 Firebase Observable to promise doesn't return anything

纵饮孤独 提交于 2019-11-30 06:49:16

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!