重新设置syncState不会更新Firebase数据 [英] Rebase syncState does not update Firebase data

查看:64
本文介绍了重新设置syncState不会更新Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mobx + Reactjs + Firebase + Re-base

I'm using Mobx + Reactjs + Firebase + Re-base

我的base.js如下:

I have my base.js like:

import firebase from 'firebase';
import Rebase from 're-base';
import 'firebase/database';

var app = firebase.initializeApp({
    apiKey: "xxxxxx",
    authDomain: "xxxxxx",
    databaseURL: "xxxxx",
    projectId: "xxxxxx",
    storageBucket: "xxxxx",
    messagingSenderId: "xxxxxx"
  });

  export var db = firebase.database(app);
  var base = Rebase.createClass(db);

  export default base;

在我的TodoList.jsx

On my TodoList.jsx

@observer
class TodoList extends React.Component {

  constructor(props){
    super(props);

    this.state={
      todos: []
    };

    this.todosRef = base.syncState(`todos`,{
      context: this,
      state:'todos',
      asArray: true,
      then: function(spa){
        console.log("Success!", spa);
      },
      onFailure: function(){
        console.log("Failed!");
      }
    });


    //this.setState({
    //  todos: this.props.store.todos
    //});

    console.log("this.state.todos", this.state.todos);
  }
  ...........

我还做了什么:

我运行了firebase-cli命令:

I ran firebase-cli commands:

firebase初始化(我选择了单页"应用程序,将数据库规则设置为读写"以用于测试)它会创建几个文件.

firebase init (I chose Single-Page app, set database rules read and write true for testing purpose) It creates several files.

但是无论我如何更改代码,Web应用程序中的数据都不会更新到Firebase数据库中.

But no matter how I change my code, the data from my web app, doesn't update into my Firebase database.

推荐答案

  export var db = firebase.database(app);

应为:

export var db = app.database();

您的 syncState 内容应放在 componentDidMount(){...} 中.

这篇关于重新设置syncState不会更新Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆