【问题标题】:base.database() is not a function error in firebase with reactbase.database() 不是具有反应的firebase中的函数错误
【发布时间】:2021-03-28 14:17:37
【问题描述】:

我已将我的应用的身份验证部分与 Google 一起使用,但我无法获取数据库数据的快照以便之后使用。

我想登录,收集用户信息,如果数据没有所有者,则将当前用户设置为所有者。

这里是认证sn -p

  login() {
    auth.signInWithPopup(provider)
      .then((result) => {
        const user = result.user;
        this.setState({
          user
        });
    });
    const storeRef = base.database().ref(this.props.storeId);
    storeRef.once("value")
      .then((snapshot) => {
        const data = snapshot.val() || {};
        if(!data.owner) {
          storeRef.set({
            owner: this.user
          });
        }
    });
  }

还有完整的组件

import React from 'react';
import AddFishForm from './AddFishForm';
import PropTypes from 'prop-types';
import base, { auth, provider } from '../base';
import * as firebase from 'firebase';

export default class Inventory extends React.Component {
  constructor() {
    super();
    this.renderInventory = this.renderInventory.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.renderLogin = this.renderLogin.bind(this);
    this.login = this.login.bind(this);
    this.logout = this.logout.bind(this);
    this.state = {
      user: null,
      owner: null
    }
  }

  handleChange(e, key) {
    const fish = this.props.fishes[key];
    const updatedFish = {
      ...fish,
      [e.target.name]: e.target.value
     }
     this.props.updateFish(key, updatedFish);
  }

  login() {
    auth.signInWithPopup(provider)
      .then((result) => {
        const user = result.user;
        this.setState({
          user
        });
    });
    const storeRef = base.database().ref(this.props.storeId);
    storeRef.once("value")
      .then((snapshot) => {
        const data = snapshot.val() || {};
        if(!data.owner) {
          storeRef.set({
            owner: this.user
          });
        }
    });
  }

  logout() {

  }

  renderLogin() {
    return (
      <nav className="login">
        <h2>Inventory</h2>
        <p>Sign in to manage your store's inventory</p>
        <button className="facebook" onClick={() => this.login()}>
          Log in with Google</button>
      </nav>
    )
  }

    renderInventory(key) {
      const fish = this.props.fishes[key];
        return(
            <div className="fish-edit" key={key}>
                <input type="text" name="name" value={fish.name} placeholder="Fish Name"
                  onChange={(e) => this.handleChange(e, key)} />
                <input type="text" name="price" value={fish.price} placeholder="Fish Price"
                  onChange={(e) => this.handleChange(e, key)} />
                <select type="text" name="status" value={fish.status} placeholder="Fish Status"
                  onChange={(e) => this.handleChange(e, key)}>
                  <option value="available">Fresh!</option>
                  <option value="unaivilable">Sold Out!</option>
                </select>
                <textarea type="text" name="desc" value={fish.desc} placeholder="Fish Desc"
                  onChange={(e) => this.handleChange(e, key)}></textarea>
                <input type="text" name="image" value={fish.image} placeholder="Fish Image"
                  onChange={(e) => this.handleChange(e, key)}/>
                <button onClick={() => this.props.removeFish(key)}>Remove Fish</button>
            </div>
        );
    }

    render() {
      const logout = <button>Log out!</button>

      if(!this.state.uid) {
        return <div>{this.renderLogin()}</div>
      }
      if(this.state.uid !== this.state.owner) {
        return(
          <div>
            <p>Sorry, you are not the owner of this store!</p>
            {logout}
          </div>
        )
      }
        return(
            <div>
                <h2>Inventory</h2>
                {logout}
                {Object.keys(this.props.fishes).map(this.renderInventory)}
                <AddFishForm addFish={this.props.addFish} />
                <button onClick={this.props.loadSamples}>Load Sample Fishes</button>
            </div>

        )
    }
}

Inventory.propTypes = {
updateFish: PropTypes.func.isRequired,
fishes: PropTypes.object.isRequired,
removeFish: PropTypes.func.isRequired,
addFish: PropTypes.func.isRequired,
loadSamples: PropTypes.func.isRequired,
storeId: PropTypes.string.isRequired
}

如果需要,也可以将 firebase 对象作为基础

import Rebase from 're-base';
import * as firebase from 'firebase';

const app = firebase.initializeApp({
  apiKey: "AIzaSyC1UGLssKKLkKvYRp02zYVpM1-D88czp7I",
  authDomain: "catch-of-the-day-ethan-fie.firebaseapp.com",
  databaseURL: "https://catch-of-the-day-ethan-fie.firebaseio.com",
  projectId: "catch-of-the-day-ethan-fie",
  storageBucket: "catch-of-the-day-ethan-fie.appspot.com",
});

const base = Rebase.createClass(app.database());

export const provider = new firebase.auth.GoogleAuthProvider();
export const auth = firebase.auth();
export default base;

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您使用 Rebase 实例而不是 firebase 实例,以修复您的问题从初始化代码导出 firebase 实例

    import Rebase from 're-base';
    import * as firebase from 'firebase';
    
    const app = firebase.initializeApp({
      apiKey: "AIzaSyC1UGLssKKLkKvYRp02zYVpM1-D88czp7I",
      authDomain: "catch-of-the-day-ethan-fie.firebaseapp.com",
      databaseURL: "https://catch-of-the-day-ethan-fie.firebaseio.com",
      projectId: "catch-of-the-day-ethan-fie",
      storageBucket: "catch-of-the-day-ethan-fie.appspot.com",
    });
    
    const base = Rebase.createClass(app.database());
    
    export const provider = new firebase.auth.GoogleAuthProvider();
    export const auth = firebase.auth();
    export const firebase = app;    //   <------ export firebase
    export default base;
    

    并在您的组件中导入 firebase

    import base, { auth, provider , firebase } from '../base';
    

    现在使用firebase 而不是base

    const storeRef = firebase.database().ref(this.props.storeId);
    storeRef.once("value")
      .then((snapshot) => {
        const data = snapshot.val() || {};
        console.log(data);
        if(!data.owner) {
          storeRef.set({
            owner: this.user
          });
        }
    });
    

    【讨论】:

    • 我很高兴你今天也在线! Base,firebase auth 让我很困惑。不过,这确实奏效了。
    • 很高兴为您提供帮助:)
    【解决方案2】:

    对我来说,在最近的更新中我解决了这个问题

    var firebase = require('firebase/app');
    require('firebase/database');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 2021-03-03
      • 2021-12-20
      • 1970-01-01
      • 2018-01-28
      • 2021-11-30
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多