【问题标题】:How to read specific collection in firebase?如何阅读firebase中的特定集合?
【发布时间】:2020-04-16 11:36:45
【问题描述】:

我有两个组件作为“用户”和“工人”登录。它们的数据在两个集合名称中分别为“用户”、“工人”。这是我的“用户登录”功能。

SignIn(email, password) {
    this.value = this.afs.collection( 'users'  , ref =>
      ref.where('email', '==', email)
    ).valueChanges();
    this.value.subscribe(data => {
      this.datas = data;
      console.log(this.datas )
        return this.afAuth.auth.signInWithEmailAndPassword(email, password).then((result) => {
          this.ngZone.run(() => {
            this.router.navigate(['home']);
          });
        }).catch((error) => {
          window.alert(error);
          this.router.navigateByUrl('signinu');
        })
    });
  }

所以在这个功能中,我可以使用“工人”收集数据登录。这意味着“工人”可以使用“用户登录”组件登录。那么如何解决这个问题呢?请帮帮我。

这是我的“工人登录”功能

SignIn(email, password) {
        this.value = this.afs.collection('workers', ref =>
            ref.where('email', '==', email)
        ).valueChanges();
        this.value.subscribe(data => {
            this.datas = data;
            console.log(this.datas)
            if (this.datas !== null) {
                return this.afAuth.auth.signInWithEmailAndPassword(email, password).then((result) => {
                    this.ngZone.run(() => {
                        this.router.navigate(['/profile/' + this.datas[0].uid]);
                    });
                }).catch((error) => {
                    window.alert(error);
                    this.router.navigateByUrl('signinu');
                })
            } else {
                window.alert('Invalid user');
            }
        });
    }

【问题讨论】:

  • 我不清楚这里有什么问题。
  • 我是工人。所以现在我以工人身份登录。当我点击“登录”按钮时,我应该进入我的“工人资料”页面。如果我登录时是用户,我应该转到“主页”。所以在这里它不起作用。即使他是“工人”或“用户”,也始终指向“主页”。
  • @DougStevenson clear?

标签: angularjs firebase google-cloud-firestore


【解决方案1】:

在登录页面中,创建一个下拉菜单,用户可以在其中选择他是user 还是worker,然后在signin 方法中添加另一个名为type 的参数:

SignIn(email, password, type)

并检查类型是否等于user,然后使用集合user,否则如果类型等于worker,则使用集合worker。当您检索时,您已经在使用where 来查询电子邮件是否存在于user 集合或worker 集合中。如果用户是worker 并且他们点击了user,那么由于您正在执行查询以检查电子邮件,因此不会检索到任何内容并且您可以显示警报。

【讨论】:

  • 我已经这么认为了,但是登录页面是正式的吗?无论如何,我会试试这个,谢谢。
  • 在下拉菜单中如何将选定的值传递给登录功能?
猜你喜欢
  • 1970-01-01
  • 2019-06-26
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多