You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Polymerfire构建的PSK PWA中调用Firebase Auth的fetchSignInMethodsForEmail()

解决Polymerfire中调用fetchSignInMethodsForEmail()的错误问题

看起来你在使用Polymerfire基于PSK构建PWA时,调用Firebase Auth的fetchSignInMethodsForEmail()方法遇到了麻烦。这个错误本质上是因为没有正确适配Polymerfire对Firebase SDK的封装逻辑,下面给你几个可行的解决思路:

1. 通过firebase-auth元素的auth属性访问原生Auth实例

Polymerfire的<firebase-auth>元素会把初始化后的原生Firebase Auth实例暴露在它的auth属性上,但你得等元素完成初始化后再调用方法——最好的方式是监听它的auth-ready事件:

// 假设你的<firebase-auth>元素id为"auth"
this.$.auth.addEventListener('auth-ready', () => {
  this.$.auth.auth.fetchSignInMethodsForEmail(email)
    .then((methods) => {
      // 在这里处理返回的登录方式数组,比如判断邮箱是否已注册
      console.log('该邮箱支持的登录方式:', methods);
    })
    .catch((error) => {
      console.error('获取登录方式失败:', error);
    });
});

千万不要跳过auth-ready事件的监听,否则auth属性可能还未被赋值,会引发新的错误。

2. 检查Polymerfire与Firebase SDK的版本兼容性

如果上面的方法还是报错,可能是你使用的Polymerfire版本和Firebase SDK版本不兼容。fetchSignInMethodsForEmail()是Firebase Auth Web SDK的标准方法,一般v5及以上版本都支持,但需要确保Polymerfire的版本能适配你引入的Firebase SDK。

你可以查看项目的bower.json(旧版Polymer项目)或package.json,确认两者的版本匹配度,必要时升级到兼容的版本组合。

3. 避免直接调用全局firebase对象

你之前尝试的firebase.auth().fetchSignInMethodsForEmail(email)写法,在Polymerfire场景下容易出问题——因为Polymerfire会自行初始化Firebase应用,全局的firebase实例可能和Polymerfire内部的实例不一致,导致方法调用失败。优先通过<firebase-auth>元素的auth属性访问Auth实例是更稳妥的选择。

另外你试过的firebase.auth.fetchSignInMethodsForEmail(email)写法本身就是错误的,auth()是一个需要调用的方法,必须先通过firebase.auth()获取实例才能调用后续方法,但同样不推荐在Polymerfire项目中这么做。


内容的提问来源于stack exchange,提问作者Andrew See

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:53:24