我正在使用Firebase Auth和VueJS,我需要将anonymous auth user转换为registered one with Google.

我使用的代码来自一个示例:

  fromAnonymousToGoogle: function () {
  // Authenticate with the first user then save the currentUser to a local variable
    var previousUser = Firebase.auth().currentUser

  // Authenticate with a second method and get a credential
    var credential = Firebase.auth.GoogleAuthProvider()

    previousUser.link(credential)
    .catch(function (error) {
     // Linking will often fail if the account has already been linked. Handle these cases manually.
      alert(error)
    })

    // OAuth providers authenticate in an asynchronous manner, so you’ll want to perform the link account link in the callback.
    // previousUser = Firebase.auth().currentUser;
    Firebase.auth().signInWithPopup(new Firebase.auth.GoogleAuthProvider())
     .then(function (result) {
       return previousUser.link(result.credential)
     })
     .catch(function (err) {
       // Handle error
       alert(err)
     })
  },

我试图将该帐户链接到谷歌时出现此错误:

[Vue warn]:"单击"的事件处理程序出错:"类型错误:this.ta不是函数"

我的代码中没有名为this.ta的函数.如何修复此错误?

推荐答案

为了回答上一个问题,您遇到此错误的原因是,您实际上并没有获得一个带有var credential = Firebase.auth.GoogleAuthProvider()的凭据,而是一个提供程序ID.因此,当您try 使用提供程序ID创建link()时,它根本不起作用(我验证了由于您代码的这一部分而遇到相同的错误).

实际上,您不想使用Google凭据登录用户,因为这会注销您的匿名用户并使用Google登录.您只需要将当前用户与一些Google凭据链接起来,您只需使用linkWithPopup方法即可完成(我重命名了变量以使其更有意义).

fromAnonymousToGoogle: function () {
            // Authenticate with the first user then save the currentUser to a local variable
            var anonUser = Firebase.auth().currentUser

            // Authenticate with a second method and get a credential
            var provider = new Firebase.auth.GoogleAuthProvider();

            anonUser.linkWithPopup(provider).then(function(result) {
                googleToken = result.credential;
                console.log(googleToken);
            }).catch(function(error) {
                console.error("Google sign in failed", error);
            });
    },

在我自己进行了测试之后,这似乎是一种使用popup链接它们的方法,它与您最初提供的代码最为匹配.

Vue.js相关问答推荐

TailwindCSS 为什么前者比后者重要?

拖入 vue2-google-map 后如何获取标记位置?

CssSyntaxError 使用 Webpack 4 构建未知单词

使用 Laravel 作为后端刷新 Vue.js SPA 的身份验证令牌

Nuxt.js 中的开发工具样式编辑问题

Vue index.html favicon 问题

如何在 Vue.js 中获取完整的当前日期和时间,而无需编写大量 JavaScript 行

Vuetify v-data-table ,如何在 HTML 中呈现标题文本?

VueJS + VueRouter:有条件地禁用路由

Vuetify / Offline离线图标

不需要提交Mutations的 vuex 操作

如何将 v-if 与 Vue.js 中的值进行比较

如何在 Vue.js 3 中使用 Vue 3 Meta?

如何更改 Vuetify 日历日期格式

vue-cli-service build --target lib 导入为 lib 时丢失图像路径

如何从组件内的单点捕获 vuejs 错误

将登录页面包含在单页应用程序中是否不安全?

何时使用

mount() 仅在组件 Vue.js 上运行一次

Vuex 模块Mutations