gpt4 book ai didi

javascript - React - Firebase - GoogleAuthProvider 不是构造函数

转载 作者:可可西里 更新时间:2023-11-01 01:58:22 25 4
gpt4 key购买 nike

我正在尝试创建一个 React 应用程序,它使用“使用 Google 登录”按钮来触发 signInWithPopup(provider)。但是,每次我调用 new firebaseApp.auth.GoogleAuthProvider() 时,我的控制台都会返回一个错误。我只是想 console.log() result

错误

Uncaught TypeError: _firebase_setup2.default.auth.GoogleAuthProvider is not a constructor

firebase_setup.js
import * as firebase from 'firebase';

const firebaseConfig = {
apiKey: "AIzaSyAKlWtFZvbvuNy2qC68Xt5xzaTQVyy9l2o",
authDomain: "doordash-ff045.firebaseapp.com",
databaseURL: "https://doordash-ff045.firebaseio.com",
storageBucket: "doordash-ff045.appspot.com",
};

const firebaseApp = firebase.initializeApp(firebaseConfig);

export default firebaseApp;
登录.js
import React, { Component } from 'react';
import { browserHistory } from 'react-router';
import firebaseApp from '../../../services/firebase_setup';

export default class Login extends Component {

componentDidMount() {
firebaseApp.auth().onAuthStateChanged(user => {
if (user) {
console.log(user);
browserHistory.push('/profile');
}
});
}

authenticate() {
var provider = new firebaseApp.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');

firebaseApp.auth().signInWithPopup(provider)
.then(result => {
console.log(result);
})
}

render() {
return (
<div>
<h1>Login Page</h1>
<button onClick={this.authenticate.bind(this)}>
Login with Google
</button>
</div>

);
}
}

如果您对此问题有任何见解,我将不胜感激!我查阅了许多教程,但在尝试声明 provider 变量时总是遇到同样的错误。

最佳答案

您将命名空间与实例混合在一起:firebaseApp 只是配置数据的容器。这不是您创建提供程序实例的方式。

正确的做法是:

var provider = new firebase.auth.GoogleAuthProvider();

关于javascript - React - Firebase - GoogleAuthProvider 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com