无法从Firebase保存或获取数据(React Native和Firebase的新增功能)

时间:2018-09-17 14:17:56

标签: javascript reactjs firebase react-native firebase-realtime-database

所以我有一个日记应用程序,试图在React Native中编写代码。我是新来的要做出反应的人,但是我读到的所有内容都表明我在做的事应该起作用。基本上,我将一些数据放入redux Action类中并尝试编写。但我不断得到,

未定义不是“评估'_firebase2.default.database.ref')的对象

我已经遍历了我多次使用的教程上的“将新数据保存到Firebase”部分,但是我并没有弄错我做错了什么。这是我的代码,

import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
import { DIARY_UPDATE, DIARY_SAVED, DIARY_FETCH_SUCCESS } from './types';


export const diaryUpdate = ({ prop, value }) => ({
    type: DIARY_UPDATE,
    payload: { prop, value }
  });

export const diarySave = ({ date, location, activity, satisfaction,
  amenities, note }) => {
    const { currentUser } = firebase.auth();

    return (dispatch) => {
      firebase.datebase.ref(`Users/${currentUser.uid}/diary`)
      .push({ date, location, activity, satisfaction, amenities, note })
      .then(() => {
          dispatch({ type: DIARY_SAVED });
          Actions.dashboard({ type: 'Replace' });
      });
    };
  };

  export const diaryFetch = () => {
      const { currentUser } = firebase.auth();

      return (dispatch) => {
        firebase.datebase.ref(`Users/${currentUser.uid}/diary`)
        .on('value', snapshot => {
          dispatch({ type: DIARY_FETCH_SUCCESS, payload: snapshot.val() });
        });
      };
    };

我有控制台日志值,以检查所传递的内容是否有问题。

这是我正在初始化Firebase的App.js。我应该提到firebase的身份验证功能可以正常工作!

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import firebase from 'firebase';
import reduxThunk from 'redux-thunk';
import reducers from './src/reducers';
import Router from './src/Router';

export default class App extends Component {

  componentWillMount() {
    if (!firebase.apps.length) {
      const fconfig = {
        apiKey: 'AIzaSyAkYD2TodRFEiln8JZPp2DtDyfIwIr654U',
        authDomain: 'yourhappyplace-6c049.firebaseapp.com',
        databaseURL: 'https://yourhappyplace-6c049.firebaseio.com',
        projectId: 'yourhappyplace-6c049',
        storageBucket: 'yourhappyplace-6c049.appspot.com',
        messagingSenderId: '634967086167'
      };
      firebase.initializeApp(fconfig);
    }
  }

  render() {
    //Create redux store with redux thunk middleware
    const happyStore = createStore(reducers, {}, applyMiddleware(reduxThunk));

    return (
      <Provider store={happyStore}>
          <Router />
      </Provider>
    );
  }
}

有人可以看到我到底在做什么错吗?谢谢!

1 个答案:

答案 0 :(得分:1)

要访问FirebaseDatabase实例,请在database()实例上调用FirebaseApp 方法

因此拼写为database()并带有括号:

firebase.database().ref(...)