【问题标题】:using firebase database trigger with react使用带有反应的firebase数据库触发器
【发布时间】:2019-10-03 02:58:23
【问题描述】:

当我的 firebase 数据库更新时,我试图在我的 React 网络应用程序中创建一个事件。

import fire from '../../config/Fire';
....
componentDidMount = () => {
    let self = this
    let database = fire.database().ref();
    let userId = this.state.userId
    database.onUpdate(event => {
        console.log('eve', event)
    })
}

在我的componentDidMount 中,我只是简单地尝试控制台记录它以查看它是否有效,但我收到一个错误:TypeError: database.onUpdate is not a function

我关注了这个页面:https://firebase.google.com/docs/functions/database-events?hl=en

但它并不完全适用于 React。

我想知道我必须如何使用它。

编辑

fire.js

import firebase from 'firebase';


const fire = firebase.initializeApp(config);
export default fire;

我的配置包含 API 密钥。

【问题讨论】:

  • 您能分享一下config/Fire 中的内容吗? fire 到底是什么?
  • @HristoEftimov 我编辑了我的问题。 fire.js 看起来像这样。

标签: reactjs firebase


【解决方案1】:

我刚刚遇到了这个问题,但在我看来,您没有正确调用 firebase 对象。这就是您收到未定义函数错误的原因。虽然您确实导入了它,但我不会设置 fire = firebase.initializeApp(config) 您需要做的就是: import * as fire from 'firebase'; 然后您可以调用您的 firebase 函数和引用,例如: fire.database().ref();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 2018-07-29
    • 2020-06-23
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多